| | | 1 | | // <copyright file="ObjFileReader9.cs" company="Jérémy Ansel"> |
| | | 2 | | // Copyright (c) 2017, 2019 Jérémy Ansel |
| | | 3 | | // </copyright> |
| | | 4 | | // <license> |
| | | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | | 6 | | // </license> |
| | | 7 | | |
| | | 8 | | #if NET6_0_OR_GREATER |
| | | 9 | | |
| | | 10 | | using System.Diagnostics.CodeAnalysis; |
| | | 11 | | using System.Globalization; |
| | | 12 | | using System.Text; |
| | | 13 | | |
| | | 14 | | #if NET9_0_OR_GREATER |
| | | 15 | | using SpanSplitEnumerator = System.MemoryExtensions.SpanSplitEnumerator<char>; |
| | | 16 | | #else |
| | | 17 | | using SpanSplitEnumerator = Polyfills.Polyfill.SpanSplitEnumerator<char>; |
| | | 18 | | #endif |
| | | 19 | | |
| | | 20 | | namespace JeremyAnsel.Media.WavefrontObj |
| | | 21 | | { |
| | | 22 | | internal static class ObjFileReader9 |
| | | 23 | | { |
| | | 24 | | private static void MoveNextSkipEmpty(ref SpanSplitEnumerator values) |
| | | 25 | | { |
| | 6825 | 26 | | while (values.MoveNext()) |
| | | 27 | | { |
| | 6825 | 28 | | if (values.Current.Start.Value != values.Current.End.Value) |
| | | 29 | | { |
| | 6780 | 30 | | return; |
| | | 31 | | } |
| | | 32 | | } |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | private static ReadOnlySpan<char> GetNextValue(ref ReadOnlySpan<char> currentLine, ref SpanSplitEnumerator value |
| | | 36 | | { |
| | 4623 | 37 | | MoveNextSkipEmpty(ref values); |
| | 4623 | 38 | | return currentLine[values.Current]; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private static float FloatParse(ReadOnlySpan<char> s) |
| | | 42 | | { |
| | 2982 | 43 | | return float.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | private static int IntParse(ReadOnlySpan<char> s) |
| | | 47 | | { |
| | 1107 | 48 | | return int.Parse(s, NumberStyles.Integer, CultureInfo.InvariantCulture); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | private static long LongParse(ReadOnlySpan<char> s) |
| | | 52 | | { |
| | 18 | 53 | | return long.Parse(s, NumberStyles.Integer, CultureInfo.InvariantCulture); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | | 57 | | public static ObjFile FromStream(Stream? stream, ObjFileReaderSettings settings) |
| | | 58 | | { |
| | 858 | 59 | | if (stream == null) |
| | | 60 | | { |
| | 3 | 61 | | throw new ArgumentNullException(nameof(stream)); |
| | | 62 | | } |
| | | 63 | | |
| | 855 | 64 | | var obj = new ObjFile(); |
| | 855 | 65 | | var context = new ObjFileReaderContext(obj, settings); |
| | 855 | 66 | | var lineReader = new LineReader9(); |
| | | 67 | | |
| | 855 | 68 | | int valueBufferSize = 16; |
| | 855 | 69 | | Span<char> valueBuffer = stackalloc char[valueBufferSize]; |
| | | 70 | | |
| | 5520 | 71 | | foreach (var currentLineString in lineReader.Read9(stream)) |
| | | 72 | | { |
| | 2157 | 73 | | ReadOnlySpan<char> currentLine = currentLineString.Buffer.AsSpan()[..currentLineString.Length]; |
| | | 74 | | |
| | 2157 | 75 | | int valuesCount = 0; |
| | | 76 | | |
| | 19488 | 77 | | foreach (Range range in currentLine.SplitAny(LineReader9.LineSeparators)) |
| | | 78 | | { |
| | 7587 | 79 | | if (currentLine[range].Length == 0) |
| | | 80 | | { |
| | | 81 | | continue; |
| | | 82 | | } |
| | | 83 | | |
| | 7500 | 84 | | valuesCount++; |
| | | 85 | | } |
| | | 86 | | |
| | 2157 | 87 | | SpanSplitEnumerator values = currentLine.SplitAny(LineReader9.LineSeparators); |
| | | 88 | | |
| | 2157 | 89 | | MoveNextSkipEmpty(ref values); |
| | | 90 | | |
| | 2157 | 91 | | if (values.Current.End.Value - values.Current.Start.Value > valueBufferSize) |
| | | 92 | | { |
| | | 93 | | continue; |
| | | 94 | | } |
| | | 95 | | |
| | 2154 | 96 | | ReadOnlySpan<char> value0 = currentLine[values.Current]; |
| | 2154 | 97 | | int value0Length = value0.ToLowerInvariant(valueBuffer); |
| | | 98 | | |
| | | 99 | | //if (value0Length == -1) |
| | | 100 | | //{ |
| | | 101 | | // throw new InvalidDataException("the buffer is too small"); |
| | | 102 | | //} |
| | | 103 | | |
| | 2154 | 104 | | switch (valueBuffer[..value0Length]) |
| | | 105 | | { |
| | | 106 | | case "v": |
| | | 107 | | { |
| | 507 | 108 | | if (valuesCount < 4) |
| | | 109 | | { |
| | 9 | 110 | | throw new InvalidDataException("A v statement must specify at least 3 values."); |
| | | 111 | | } |
| | | 112 | | |
| | 498 | 113 | | float x = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 498 | 114 | | float y = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 498 | 115 | | float z = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 498 | 116 | | float w = 1.0f; |
| | 498 | 117 | | bool hasColor = false; |
| | 498 | 118 | | float r = 0.0f; |
| | 498 | 119 | | float g = 0.0f; |
| | 498 | 120 | | float b = 0.0f; |
| | 498 | 121 | | float a = 1.0f; |
| | | 122 | | |
| | 498 | 123 | | if (valuesCount == 4 || valuesCount == 5) |
| | | 124 | | { |
| | 486 | 125 | | if (valuesCount == 5) |
| | | 126 | | { |
| | 3 | 127 | | w = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 128 | | } |
| | | 129 | | } |
| | 12 | 130 | | else if (valuesCount == 7 || valuesCount == 8) |
| | | 131 | | { |
| | 6 | 132 | | hasColor = true; |
| | 6 | 133 | | r = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 6 | 134 | | g = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 6 | 135 | | b = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 136 | | |
| | 6 | 137 | | if (valuesCount == 8) |
| | | 138 | | { |
| | 3 | 139 | | a = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | else |
| | | 143 | | { |
| | 6 | 144 | | throw new InvalidDataException("A v statement has too many values."); |
| | | 145 | | } |
| | | 146 | | |
| | 492 | 147 | | var v = new ObjVertex(); |
| | 492 | 148 | | v.Position = new ObjVector4(x, y, z, w); |
| | | 149 | | |
| | 492 | 150 | | if (hasColor) |
| | | 151 | | { |
| | 6 | 152 | | v.Color = new ObjVector4(r, g, b, a); |
| | | 153 | | } |
| | | 154 | | |
| | 492 | 155 | | obj.Vertices.Add(v); |
| | 492 | 156 | | break; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | case "vp": |
| | | 160 | | { |
| | 180 | 161 | | if (valuesCount < 2) |
| | | 162 | | { |
| | 3 | 163 | | throw new InvalidDataException("A vp statement must specify at least 1 value."); |
| | | 164 | | } |
| | | 165 | | |
| | 177 | 166 | | var v = new ObjVector3(); |
| | 177 | 167 | | v.X = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 168 | | |
| | 177 | 169 | | if (valuesCount == 2) |
| | | 170 | | { |
| | 3 | 171 | | v.Y = 0.0f; |
| | 3 | 172 | | v.Z = 1.0f; |
| | | 173 | | } |
| | 174 | 174 | | else if (valuesCount == 3) |
| | | 175 | | { |
| | 3 | 176 | | v.Y = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 177 | | v.Z = 1.0f; |
| | | 178 | | } |
| | 171 | 179 | | else if (valuesCount == 4) |
| | | 180 | | { |
| | 168 | 181 | | v.Y = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 168 | 182 | | v.Z = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 183 | | } |
| | | 184 | | else |
| | | 185 | | { |
| | 3 | 186 | | throw new InvalidDataException("A vp statement has too many values."); |
| | | 187 | | } |
| | | 188 | | |
| | 174 | 189 | | obj.ParameterSpaceVertices.Add(v); |
| | 174 | 190 | | break; |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | case "vn": |
| | | 194 | | { |
| | 36 | 195 | | if (valuesCount < 4) |
| | | 196 | | { |
| | 9 | 197 | | throw new InvalidDataException("A vn statement must specify 3 values."); |
| | | 198 | | } |
| | | 199 | | |
| | 27 | 200 | | if (valuesCount != 4) |
| | | 201 | | { |
| | 3 | 202 | | throw new InvalidDataException("A vn statement has too many values."); |
| | | 203 | | } |
| | | 204 | | |
| | 24 | 205 | | var v = new ObjVector3(); |
| | 24 | 206 | | v.X = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 24 | 207 | | v.Y = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 24 | 208 | | v.Z = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 209 | | |
| | 24 | 210 | | obj.VertexNormals.Add(v); |
| | 24 | 211 | | break; |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | case "vt": |
| | | 215 | | { |
| | 33 | 216 | | if (valuesCount < 2) |
| | | 217 | | { |
| | 3 | 218 | | throw new InvalidDataException("A vt statement must specify at least 1 value."); |
| | | 219 | | } |
| | | 220 | | |
| | 30 | 221 | | var v = new ObjVector3(); |
| | 30 | 222 | | v.X = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 223 | | |
| | 30 | 224 | | if (valuesCount == 2) |
| | | 225 | | { |
| | 21 | 226 | | v.Y = 0.0f; |
| | 21 | 227 | | v.Z = 0.0f; |
| | | 228 | | } |
| | 9 | 229 | | else if (valuesCount == 3) |
| | | 230 | | { |
| | 3 | 231 | | v.Y = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 232 | | v.Z = 0.0f; |
| | | 233 | | } |
| | 6 | 234 | | else if (valuesCount == 4) |
| | | 235 | | { |
| | 3 | 236 | | v.Y = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 237 | | v.Z = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 238 | | } |
| | | 239 | | else |
| | | 240 | | { |
| | 3 | 241 | | throw new InvalidDataException("A vt statement has too many values."); |
| | | 242 | | } |
| | | 243 | | |
| | 27 | 244 | | obj.TextureVertices.Add(v); |
| | 27 | 245 | | break; |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | case "cstype": |
| | 45 | 249 | | ParseFreeFormType(context, ref value0, ref currentLine, ref values, valuesCount); |
| | 30 | 250 | | break; |
| | | 251 | | |
| | | 252 | | case "deg": |
| | 21 | 253 | | if (valuesCount < 2) |
| | | 254 | | { |
| | 3 | 255 | | throw new InvalidDataException("A deg statement must specify at least 1 value."); |
| | | 256 | | } |
| | | 257 | | |
| | 18 | 258 | | if (valuesCount == 2) |
| | | 259 | | { |
| | 3 | 260 | | context.DegreeU = IntParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 261 | | context.DegreeV = 0; |
| | | 262 | | } |
| | 15 | 263 | | else if (valuesCount == 3) |
| | | 264 | | { |
| | 12 | 265 | | context.DegreeU = IntParse(GetNextValue(ref currentLine, ref values)); |
| | 12 | 266 | | context.DegreeV = IntParse(GetNextValue(ref currentLine, ref values)); |
| | | 267 | | } |
| | | 268 | | else |
| | | 269 | | { |
| | 3 | 270 | | throw new InvalidDataException("A deg statement has too many values."); |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | break; |
| | | 274 | | |
| | | 275 | | case "bmat": |
| | | 276 | | { |
| | 15 | 277 | | if (valuesCount < 2) |
| | | 278 | | { |
| | 3 | 279 | | throw new InvalidDataException("A bmat statement must specify a direction."); |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | int d; |
| | 12 | 283 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 284 | | |
| | 12 | 285 | | if (value1.Equals("u", StringComparison.OrdinalIgnoreCase)) |
| | | 286 | | { |
| | 6 | 287 | | d = 1; |
| | | 288 | | } |
| | 6 | 289 | | else if (value1.Equals("v", StringComparison.OrdinalIgnoreCase)) |
| | | 290 | | { |
| | 3 | 291 | | d = 2; |
| | | 292 | | } |
| | | 293 | | else |
| | | 294 | | { |
| | 3 | 295 | | throw new InvalidDataException("A bmat statement has an unknown direction."); |
| | | 296 | | } |
| | | 297 | | |
| | 9 | 298 | | int count = (context.DegreeU + 1) * (context.DegreeV + 1); |
| | | 299 | | |
| | 9 | 300 | | if (valuesCount != count + 2) |
| | | 301 | | { |
| | 3 | 302 | | throw new InvalidDataException("A bmat statement has too many or too few values."); |
| | | 303 | | } |
| | | 304 | | |
| | 6 | 305 | | var matrix = new float[count]; |
| | | 306 | | |
| | 156 | 307 | | for (int i = 0; i < count; i++) |
| | | 308 | | { |
| | 72 | 309 | | matrix[i] = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | switch (d) |
| | | 313 | | { |
| | | 314 | | case 1: |
| | 3 | 315 | | context.BasicMatrixU = matrix; |
| | 3 | 316 | | break; |
| | | 317 | | |
| | | 318 | | case 2: |
| | 3 | 319 | | context.BasicMatrixV = matrix; |
| | 3 | 320 | | break; |
| | | 321 | | } |
| | | 322 | | |
| | | 323 | | break; |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | case "step": |
| | 12 | 327 | | if (valuesCount < 2) |
| | | 328 | | { |
| | 3 | 329 | | throw new InvalidDataException("A step statement must specify at least 1 value."); |
| | | 330 | | } |
| | | 331 | | |
| | 9 | 332 | | if (valuesCount == 2) |
| | | 333 | | { |
| | 3 | 334 | | context.StepU = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 335 | | context.StepV = 1.0f; |
| | | 336 | | } |
| | 6 | 337 | | else if (valuesCount == 3) |
| | | 338 | | { |
| | 3 | 339 | | context.StepU = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 340 | | context.StepV = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 341 | | } |
| | | 342 | | else |
| | | 343 | | { |
| | 3 | 344 | | throw new InvalidDataException("A step statement has too many values."); |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | break; |
| | | 348 | | |
| | | 349 | | case "p": |
| | | 350 | | { |
| | 105 | 351 | | if (valuesCount < 2) |
| | | 352 | | { |
| | 3 | 353 | | throw new InvalidDataException("A p statement must specify at least 1 value."); |
| | | 354 | | } |
| | | 355 | | |
| | 102 | 356 | | var point = new ObjPoint(); |
| | | 357 | | |
| | 366 | 358 | | for (int i = 1; i < valuesCount; i++) |
| | | 359 | | { |
| | 108 | 360 | | point.Vertices.Add(ParseTriplet(obj, GetNextValue(ref currentLine, ref values))); |
| | | 361 | | } |
| | | 362 | | |
| | 75 | 363 | | context.ApplyAttributesToElement(point); |
| | 75 | 364 | | context.ApplyAttributesToPolygonalElement(point); |
| | | 365 | | |
| | 75 | 366 | | obj.Points.Add(point); |
| | | 367 | | |
| | 300 | 368 | | foreach (var group in context.GetCurrentGroups()) |
| | | 369 | | { |
| | 75 | 370 | | group.Points.Add(point); |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | break; |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | case "l": |
| | | 377 | | { |
| | 12 | 378 | | if (valuesCount < 3) |
| | | 379 | | { |
| | 6 | 380 | | throw new InvalidDataException("A l statement must specify at least 2 values."); |
| | | 381 | | } |
| | | 382 | | |
| | 6 | 383 | | var line = new ObjLine(); |
| | | 384 | | |
| | 48 | 385 | | for (int i = 1; i < valuesCount; i++) |
| | | 386 | | { |
| | 18 | 387 | | line.Vertices.Add(ParseTriplet(obj, GetNextValue(ref currentLine, ref values))); |
| | | 388 | | } |
| | | 389 | | |
| | 6 | 390 | | context.ApplyAttributesToElement(line); |
| | 6 | 391 | | context.ApplyAttributesToPolygonalElement(line); |
| | | 392 | | |
| | 6 | 393 | | obj.Lines.Add(line); |
| | | 394 | | |
| | 24 | 395 | | foreach (var group in context.GetCurrentGroups()) |
| | | 396 | | { |
| | 6 | 397 | | group.Lines.Add(line); |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | break; |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | case "f": |
| | | 404 | | case "fo": |
| | | 405 | | { |
| | 30 | 406 | | if (valuesCount < 4) |
| | | 407 | | { |
| | 18 | 408 | | throw new InvalidDataException("A f statement must specify at least 3 values."); |
| | | 409 | | } |
| | | 410 | | |
| | 12 | 411 | | var face = new ObjFace(); |
| | | 412 | | |
| | 144 | 413 | | for (int i = 1; i < valuesCount; i++) |
| | | 414 | | { |
| | 60 | 415 | | face.Vertices.Add(ParseTriplet(obj, GetNextValue(ref currentLine, ref values))); |
| | | 416 | | } |
| | | 417 | | |
| | 12 | 418 | | context.ApplyAttributesToElement(face); |
| | 12 | 419 | | context.ApplyAttributesToPolygonalElement(face); |
| | | 420 | | |
| | 12 | 421 | | obj.Faces.Add(face); |
| | | 422 | | |
| | 48 | 423 | | foreach (var group in context.GetCurrentGroups()) |
| | | 424 | | { |
| | 12 | 425 | | group.Faces.Add(face); |
| | | 426 | | } |
| | | 427 | | |
| | | 428 | | break; |
| | | 429 | | } |
| | | 430 | | |
| | | 431 | | case "curv": |
| | | 432 | | { |
| | 135 | 433 | | if (valuesCount < 5) |
| | | 434 | | { |
| | 12 | 435 | | throw new InvalidDataException("A curv statement must specify at least 4 values."); |
| | | 436 | | } |
| | | 437 | | |
| | 123 | 438 | | var curve = new ObjCurve(); |
| | | 439 | | |
| | 123 | 440 | | curve.StartParameter = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 123 | 441 | | curve.EndParameter = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 442 | | |
| | 714 | 443 | | for (int i = 3; i < valuesCount; i++) |
| | | 444 | | { |
| | 243 | 445 | | int v = IntParse(GetNextValue(ref currentLine, ref values)); |
| | | 446 | | |
| | 243 | 447 | | if (v == 0) |
| | | 448 | | { |
| | 3 | 449 | | throw new InvalidDataException("A curv statement contains an invalid vertex index.") |
| | | 450 | | } |
| | | 451 | | |
| | 240 | 452 | | if (v < 0) |
| | | 453 | | { |
| | 3 | 454 | | v = obj.Vertices.Count + v + 1; |
| | | 455 | | } |
| | | 456 | | |
| | 240 | 457 | | if (v <= 0 || v > obj.Vertices.Count) |
| | | 458 | | { |
| | 6 | 459 | | throw new IndexOutOfRangeException(); |
| | | 460 | | } |
| | | 461 | | |
| | 234 | 462 | | curve.Vertices.Add(v); |
| | | 463 | | } |
| | | 464 | | |
| | 114 | 465 | | context.ApplyAttributesToElement(curve); |
| | 114 | 466 | | context.ApplyAttributesToFreeFormElement(curve); |
| | 114 | 467 | | context.CurrentFreeFormElement = curve; |
| | | 468 | | |
| | 114 | 469 | | obj.Curves.Add(curve); |
| | | 470 | | |
| | 456 | 471 | | foreach (var group in context.GetCurrentGroups()) |
| | | 472 | | { |
| | 114 | 473 | | group.Curves.Add(curve); |
| | | 474 | | } |
| | | 475 | | |
| | | 476 | | break; |
| | | 477 | | } |
| | | 478 | | |
| | | 479 | | case "curv2": |
| | | 480 | | { |
| | 183 | 481 | | if (valuesCount < 3) |
| | | 482 | | { |
| | 6 | 483 | | throw new InvalidDataException("A curv2 statement must specify at least 2 values."); |
| | | 484 | | } |
| | | 485 | | |
| | 177 | 486 | | var curve = new ObjCurve2D(); |
| | | 487 | | |
| | 1038 | 488 | | for (int i = 1; i < valuesCount; i++) |
| | | 489 | | { |
| | 351 | 490 | | int vp = IntParse(GetNextValue(ref currentLine, ref values)); |
| | | 491 | | |
| | 351 | 492 | | if (vp == 0) |
| | | 493 | | { |
| | 3 | 494 | | throw new InvalidDataException("A curv2 statement contains an invalid parameter spac |
| | | 495 | | } |
| | | 496 | | |
| | 348 | 497 | | if (vp < 0) |
| | | 498 | | { |
| | 3 | 499 | | vp = obj.ParameterSpaceVertices.Count + vp + 1; |
| | | 500 | | } |
| | | 501 | | |
| | 348 | 502 | | if (vp <= 0 || vp > obj.ParameterSpaceVertices.Count) |
| | | 503 | | { |
| | 6 | 504 | | throw new IndexOutOfRangeException(); |
| | | 505 | | } |
| | | 506 | | |
| | 342 | 507 | | curve.ParameterSpaceVertices.Add(vp); |
| | | 508 | | } |
| | | 509 | | |
| | 168 | 510 | | context.ApplyAttributesToElement(curve); |
| | 168 | 511 | | context.ApplyAttributesToFreeFormElement(curve); |
| | 168 | 512 | | context.CurrentFreeFormElement = curve; |
| | | 513 | | |
| | 168 | 514 | | obj.Curves2D.Add(curve); |
| | | 515 | | |
| | 672 | 516 | | foreach (var group in context.GetCurrentGroups()) |
| | | 517 | | { |
| | 168 | 518 | | group.Curves2D.Add(curve); |
| | | 519 | | } |
| | | 520 | | |
| | | 521 | | break; |
| | | 522 | | } |
| | | 523 | | |
| | | 524 | | case "surf": |
| | | 525 | | { |
| | 78 | 526 | | if (valuesCount < 6) |
| | | 527 | | { |
| | 18 | 528 | | throw new InvalidDataException("A surf statement must specify at least 5 values."); |
| | | 529 | | } |
| | | 530 | | |
| | 60 | 531 | | var surface = new ObjSurface(); |
| | | 532 | | |
| | 60 | 533 | | surface.StartParameterU = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 60 | 534 | | surface.EndParameterU = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 60 | 535 | | surface.StartParameterV = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 60 | 536 | | surface.EndParameterV = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 537 | | |
| | 252 | 538 | | for (int i = 5; i < valuesCount; i++) |
| | | 539 | | { |
| | 72 | 540 | | surface.Vertices.Add(ParseTriplet(obj, GetNextValue(ref currentLine, ref values))); |
| | | 541 | | } |
| | | 542 | | |
| | 54 | 543 | | context.ApplyAttributesToElement(surface); |
| | 54 | 544 | | context.ApplyAttributesToFreeFormElement(surface); |
| | 54 | 545 | | context.CurrentFreeFormElement = surface; |
| | | 546 | | |
| | 54 | 547 | | obj.Surfaces.Add(surface); |
| | | 548 | | |
| | 216 | 549 | | foreach (var group in context.GetCurrentGroups()) |
| | | 550 | | { |
| | 54 | 551 | | group.Surfaces.Add(surface); |
| | | 552 | | } |
| | | 553 | | |
| | | 554 | | break; |
| | | 555 | | } |
| | | 556 | | |
| | | 557 | | case "parm": |
| | | 558 | | { |
| | 21 | 559 | | if (context.CurrentFreeFormElement == null) |
| | | 560 | | { |
| | | 561 | | break; |
| | | 562 | | } |
| | | 563 | | |
| | 18 | 564 | | if (valuesCount < 4) |
| | | 565 | | { |
| | 9 | 566 | | throw new InvalidDataException("A parm statement must specify at least 3 values."); |
| | | 567 | | } |
| | | 568 | | |
| | | 569 | | List<float> parameters; |
| | 9 | 570 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 571 | | |
| | 9 | 572 | | if (value1.Equals("u", StringComparison.OrdinalIgnoreCase)) |
| | | 573 | | { |
| | 3 | 574 | | parameters = context.CurrentFreeFormElement.ParametersU; |
| | | 575 | | } |
| | 6 | 576 | | else if (value1.Equals("v", StringComparison.OrdinalIgnoreCase)) |
| | | 577 | | { |
| | 3 | 578 | | parameters = context.CurrentFreeFormElement.ParametersV; |
| | | 579 | | } |
| | | 580 | | else |
| | | 581 | | { |
| | 3 | 582 | | throw new InvalidDataException("A parm statement has an unknown direction."); |
| | | 583 | | } |
| | | 584 | | |
| | 48 | 585 | | for (int i = 2; i < valuesCount; i++) |
| | | 586 | | { |
| | 18 | 587 | | parameters.Add(FloatParse(GetNextValue(ref currentLine, ref values))); |
| | | 588 | | } |
| | | 589 | | |
| | 6 | 590 | | break; |
| | | 591 | | } |
| | | 592 | | |
| | | 593 | | case "trim": |
| | 30 | 594 | | if (context.CurrentFreeFormElement == null) |
| | | 595 | | { |
| | | 596 | | break; |
| | | 597 | | } |
| | | 598 | | |
| | 27 | 599 | | ParseCurveIndex(context.CurrentFreeFormElement.OuterTrimmingCurves, obj, value0, ref currentLine |
| | 3 | 600 | | break; |
| | | 601 | | |
| | | 602 | | case "hole": |
| | 30 | 603 | | if (context.CurrentFreeFormElement == null) |
| | | 604 | | { |
| | | 605 | | break; |
| | | 606 | | } |
| | | 607 | | |
| | 27 | 608 | | ParseCurveIndex(context.CurrentFreeFormElement.InnerTrimmingCurves, obj, value0, ref currentLine |
| | 3 | 609 | | break; |
| | | 610 | | |
| | | 611 | | case "scrv": |
| | 30 | 612 | | if (context.CurrentFreeFormElement == null) |
| | | 613 | | { |
| | | 614 | | break; |
| | | 615 | | } |
| | | 616 | | |
| | 27 | 617 | | ParseCurveIndex(context.CurrentFreeFormElement.SequenceCurves, obj, value0, ref currentLine, ref |
| | 3 | 618 | | break; |
| | | 619 | | |
| | | 620 | | case "sp": |
| | 18 | 621 | | if (context.CurrentFreeFormElement == null) |
| | | 622 | | { |
| | | 623 | | break; |
| | | 624 | | } |
| | | 625 | | |
| | 15 | 626 | | if (valuesCount < 2) |
| | | 627 | | { |
| | 3 | 628 | | throw new InvalidDataException("A sp statement must specify at least 1 value."); |
| | | 629 | | } |
| | | 630 | | |
| | 36 | 631 | | for (int i = 1; i < valuesCount; i++) |
| | | 632 | | { |
| | 15 | 633 | | int vp = IntParse(GetNextValue(ref currentLine, ref values)); |
| | | 634 | | |
| | 15 | 635 | | if (vp == 0) |
| | | 636 | | { |
| | 3 | 637 | | throw new InvalidDataException("A sp statement contains an invalid parameter space verte |
| | | 638 | | } |
| | | 639 | | |
| | 12 | 640 | | if (vp < 0) |
| | | 641 | | { |
| | 3 | 642 | | vp = obj.ParameterSpaceVertices.Count + vp + 1; |
| | | 643 | | } |
| | | 644 | | |
| | 12 | 645 | | if (vp <= 0 || vp > obj.ParameterSpaceVertices.Count) |
| | | 646 | | { |
| | 6 | 647 | | throw new IndexOutOfRangeException(); |
| | | 648 | | } |
| | | 649 | | |
| | 6 | 650 | | context.CurrentFreeFormElement.SpecialPoints.Add(vp); |
| | | 651 | | } |
| | | 652 | | |
| | 3 | 653 | | break; |
| | | 654 | | |
| | | 655 | | case "end": |
| | 18 | 656 | | context.CurrentFreeFormElement = null; |
| | 18 | 657 | | break; |
| | | 658 | | |
| | | 659 | | case "con": |
| | 66 | 660 | | ParseSurfaceConnection(obj, value0, ref currentLine, ref values, valuesCount); |
| | 3 | 661 | | break; |
| | | 662 | | |
| | | 663 | | case "g": |
| | 78 | 664 | | ParseGroupName(context, value0, ref currentLine, ref values, valuesCount); |
| | 78 | 665 | | break; |
| | | 666 | | |
| | | 667 | | case "s": |
| | | 668 | | { |
| | 27 | 669 | | if (valuesCount < 2) |
| | | 670 | | { |
| | 3 | 671 | | throw new InvalidDataException("A s statement must specify a value."); |
| | | 672 | | } |
| | | 673 | | |
| | 24 | 674 | | if (valuesCount != 2) |
| | | 675 | | { |
| | 3 | 676 | | throw new InvalidDataException("A s statement has too many values."); |
| | | 677 | | } |
| | | 678 | | |
| | 21 | 679 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 680 | | |
| | 21 | 681 | | if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 682 | | { |
| | 3 | 683 | | context.SmoothingGroupNumber = 0; |
| | | 684 | | } |
| | | 685 | | else |
| | | 686 | | { |
| | 18 | 687 | | context.SmoothingGroupNumber = LongParse(value1); |
| | | 688 | | } |
| | | 689 | | |
| | 18 | 690 | | break; |
| | | 691 | | } |
| | | 692 | | |
| | | 693 | | case "mg": |
| | | 694 | | { |
| | 30 | 695 | | if (valuesCount < 2) |
| | | 696 | | { |
| | 3 | 697 | | throw new InvalidDataException("A mg statement must specify a value."); |
| | | 698 | | } |
| | | 699 | | |
| | 27 | 700 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 701 | | |
| | 27 | 702 | | if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 703 | | { |
| | 3 | 704 | | context.MergingGroupNumber = 0; |
| | | 705 | | } |
| | | 706 | | else |
| | | 707 | | { |
| | 24 | 708 | | context.MergingGroupNumber = IntParse(value1); |
| | | 709 | | } |
| | | 710 | | |
| | 27 | 711 | | if (context.MergingGroupNumber == 0) |
| | | 712 | | { |
| | 9 | 713 | | if (valuesCount > 3) |
| | | 714 | | { |
| | 3 | 715 | | throw new InvalidDataException("A mg statement has too many values."); |
| | | 716 | | } |
| | | 717 | | } |
| | | 718 | | else |
| | | 719 | | { |
| | 18 | 720 | | if (valuesCount != 3) |
| | | 721 | | { |
| | 6 | 722 | | throw new InvalidDataException("A mg statement has too many or too few values."); |
| | | 723 | | } |
| | | 724 | | |
| | 12 | 725 | | float res = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | | 726 | | |
| | 12 | 727 | | obj.MergingGroupResolutions[context.MergingGroupNumber] = res; |
| | | 728 | | } |
| | | 729 | | |
| | 12 | 730 | | break; |
| | | 731 | | } |
| | | 732 | | |
| | | 733 | | case "o": |
| | 39 | 734 | | if (settings.HandleObjectNamesAsGroup) |
| | | 735 | | { |
| | 9 | 736 | | ParseGroupName(context, value0, ref currentLine, ref values, valuesCount); |
| | 9 | 737 | | break; |
| | | 738 | | } |
| | | 739 | | |
| | 30 | 740 | | if (valuesCount == 1) |
| | | 741 | | { |
| | 3 | 742 | | context.ObjectName = null; |
| | 3 | 743 | | break; |
| | | 744 | | } |
| | | 745 | | |
| | 27 | 746 | | if (valuesCount != 2) |
| | | 747 | | { |
| | 3 | 748 | | throw new InvalidDataException("A o statement has too many values."); |
| | | 749 | | } |
| | | 750 | | |
| | 24 | 751 | | context.ObjectName = GetNextValue(ref currentLine, ref values).ToString(); |
| | 24 | 752 | | break; |
| | | 753 | | |
| | | 754 | | case "bevel": |
| | | 755 | | { |
| | 27 | 756 | | if (valuesCount < 2) |
| | | 757 | | { |
| | 3 | 758 | | throw new InvalidDataException("A bevel statement must specify a name."); |
| | | 759 | | } |
| | | 760 | | |
| | 24 | 761 | | if (valuesCount != 2) |
| | | 762 | | { |
| | 3 | 763 | | throw new InvalidDataException("A bevel statement has too many values."); |
| | | 764 | | } |
| | | 765 | | |
| | 21 | 766 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 767 | | |
| | 21 | 768 | | if (value1.Equals("on", StringComparison.OrdinalIgnoreCase)) |
| | | 769 | | { |
| | 15 | 770 | | context.IsBevelInterpolationEnabled = true; |
| | | 771 | | } |
| | 6 | 772 | | else if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 773 | | { |
| | 3 | 774 | | context.IsBevelInterpolationEnabled = false; |
| | | 775 | | } |
| | | 776 | | else |
| | | 777 | | { |
| | 3 | 778 | | throw new InvalidDataException("A bevel statement must specify on or off."); |
| | | 779 | | } |
| | | 780 | | |
| | | 781 | | break; |
| | | 782 | | } |
| | | 783 | | |
| | | 784 | | case "c_interp": |
| | | 785 | | { |
| | 27 | 786 | | if (valuesCount < 2) |
| | | 787 | | { |
| | 3 | 788 | | throw new InvalidDataException("A c_interp statement must specify a name."); |
| | | 789 | | } |
| | | 790 | | |
| | 24 | 791 | | if (valuesCount != 2) |
| | | 792 | | { |
| | 3 | 793 | | throw new InvalidDataException("A c_interp statement has too many values."); |
| | | 794 | | } |
| | | 795 | | |
| | 21 | 796 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 797 | | |
| | 21 | 798 | | if (value1.Equals("on", StringComparison.OrdinalIgnoreCase)) |
| | | 799 | | { |
| | 15 | 800 | | context.IsColorInterpolationEnabled = true; |
| | | 801 | | } |
| | 6 | 802 | | else if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 803 | | { |
| | 3 | 804 | | context.IsColorInterpolationEnabled = false; |
| | | 805 | | } |
| | | 806 | | else |
| | | 807 | | { |
| | 3 | 808 | | throw new InvalidDataException("A c_interp statement must specify on or off."); |
| | | 809 | | } |
| | | 810 | | |
| | | 811 | | break; |
| | | 812 | | } |
| | | 813 | | |
| | | 814 | | case "d_interp": |
| | | 815 | | { |
| | 27 | 816 | | if (valuesCount < 2) |
| | | 817 | | { |
| | 3 | 818 | | throw new InvalidDataException("A d_interp statement must specify a name."); |
| | | 819 | | } |
| | | 820 | | |
| | 24 | 821 | | if (valuesCount != 2) |
| | | 822 | | { |
| | 3 | 823 | | throw new InvalidDataException("A d_interp statement has too many values."); |
| | | 824 | | } |
| | | 825 | | |
| | 21 | 826 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 827 | | |
| | 21 | 828 | | if (value1.Equals("on", StringComparison.OrdinalIgnoreCase)) |
| | | 829 | | { |
| | 15 | 830 | | context.IsDissolveInterpolationEnabled = true; |
| | | 831 | | } |
| | 6 | 832 | | else if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 833 | | { |
| | 3 | 834 | | context.IsDissolveInterpolationEnabled = false; |
| | | 835 | | } |
| | | 836 | | else |
| | | 837 | | { |
| | 3 | 838 | | throw new InvalidDataException("A d_interp statement must specify on or off."); |
| | | 839 | | } |
| | | 840 | | |
| | | 841 | | break; |
| | | 842 | | } |
| | | 843 | | |
| | | 844 | | case "lod": |
| | 30 | 845 | | if (valuesCount < 2) |
| | | 846 | | { |
| | 3 | 847 | | throw new InvalidDataException("A lod statement must specify a value."); |
| | | 848 | | } |
| | | 849 | | |
| | 27 | 850 | | if (valuesCount != 2) |
| | | 851 | | { |
| | 3 | 852 | | throw new InvalidDataException("A lod statement has too many values."); |
| | | 853 | | } |
| | | 854 | | |
| | 24 | 855 | | context.LevelOfDetail = IntParse(GetNextValue(ref currentLine, ref values)); |
| | 24 | 856 | | break; |
| | | 857 | | |
| | | 858 | | case "maplib": |
| | 6 | 859 | | if (valuesCount < 2) |
| | | 860 | | { |
| | 3 | 861 | | throw new InvalidDataException("A maplib statement must specify a file name."); |
| | | 862 | | } |
| | | 863 | | |
| | 18 | 864 | | for (int i = 1; i < valuesCount; i++) |
| | | 865 | | { |
| | 6 | 866 | | obj.MapLibraries.Add(GetNextValue(ref currentLine, ref values).ToString()); |
| | | 867 | | } |
| | | 868 | | |
| | 3 | 869 | | break; |
| | | 870 | | |
| | | 871 | | case "mtllib": |
| | | 872 | | { |
| | 18 | 873 | | if (valuesCount < 2) |
| | | 874 | | { |
| | 3 | 875 | | throw new InvalidDataException("A mtllib statement must specify a file name."); |
| | | 876 | | } |
| | | 877 | | |
| | 15 | 878 | | if (settings.KeepWhitespacesOfMtlLibReferences) |
| | | 879 | | { |
| | 6 | 880 | | obj.MaterialLibraries.Add(new string(currentLine[7..]).Trim()); |
| | | 881 | | } |
| | | 882 | | else |
| | | 883 | | { |
| | 9 | 884 | | var sb = new StringBuilder(); |
| | | 885 | | |
| | 9 | 886 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 887 | | |
| | 60 | 888 | | for (int i = 2; i < valuesCount; i++) |
| | | 889 | | { |
| | 21 | 890 | | sb.Append(' '); |
| | 21 | 891 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 892 | | } |
| | | 893 | | |
| | 9 | 894 | | obj.MaterialLibraries.Add(sb.ToString()); |
| | | 895 | | } |
| | | 896 | | |
| | 9 | 897 | | break; |
| | | 898 | | } |
| | | 899 | | |
| | | 900 | | case "usemap": |
| | | 901 | | { |
| | 33 | 902 | | if (valuesCount < 2) |
| | | 903 | | { |
| | 3 | 904 | | throw new InvalidDataException("A usemap statement must specify a value."); |
| | | 905 | | } |
| | | 906 | | |
| | 30 | 907 | | if (valuesCount != 2) |
| | | 908 | | { |
| | 3 | 909 | | throw new InvalidDataException("A usemap statement has too many values."); |
| | | 910 | | } |
| | | 911 | | |
| | 27 | 912 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 913 | | |
| | 27 | 914 | | if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 915 | | { |
| | 3 | 916 | | context.MapName = null; |
| | | 917 | | } |
| | | 918 | | else |
| | | 919 | | { |
| | 24 | 920 | | context.MapName = value1.ToString(); |
| | | 921 | | } |
| | | 922 | | |
| | 24 | 923 | | break; |
| | | 924 | | } |
| | | 925 | | |
| | | 926 | | case "usemtl": |
| | | 927 | | { |
| | 36 | 928 | | if (valuesCount < 2) |
| | | 929 | | { |
| | 3 | 930 | | throw new InvalidDataException("A usemtl statement must specify a value."); |
| | | 931 | | } |
| | | 932 | | |
| | 33 | 933 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | | 934 | | |
| | 33 | 935 | | if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | | 936 | | { |
| | 6 | 937 | | if (valuesCount != 2) |
| | | 938 | | { |
| | 3 | 939 | | throw new InvalidDataException("A usemtl statement has too many values."); |
| | | 940 | | } |
| | | 941 | | |
| | 3 | 942 | | context.MaterialName = null; |
| | | 943 | | } |
| | | 944 | | else |
| | | 945 | | { |
| | 27 | 946 | | var sb = new StringBuilder(); |
| | | 947 | | |
| | 27 | 948 | | sb.Append(value1); |
| | | 949 | | |
| | 84 | 950 | | for (int i = 2; i < valuesCount; i++) |
| | | 951 | | { |
| | 15 | 952 | | sb.Append(' '); |
| | 15 | 953 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 954 | | } |
| | | 955 | | |
| | 27 | 956 | | context.MaterialName = sb.ToString(); |
| | | 957 | | } |
| | | 958 | | |
| | 27 | 959 | | break; |
| | | 960 | | } |
| | | 961 | | |
| | | 962 | | case "shadow_obj": |
| | | 963 | | { |
| | 18 | 964 | | if (valuesCount < 2) |
| | | 965 | | { |
| | 3 | 966 | | throw new InvalidDataException("A shadow_obj statement must specify a file name."); |
| | | 967 | | } |
| | | 968 | | |
| | 15 | 969 | | var sb = new StringBuilder(); |
| | | 970 | | |
| | 15 | 971 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 972 | | |
| | 36 | 973 | | for (int i = 2; i < valuesCount; i++) |
| | | 974 | | { |
| | 3 | 975 | | sb.Append(' '); |
| | 3 | 976 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 977 | | } |
| | | 978 | | |
| | 15 | 979 | | obj.ShadowObjectFileName = sb.ToString(); |
| | 15 | 980 | | break; |
| | | 981 | | } |
| | | 982 | | |
| | | 983 | | case "trace_obj": |
| | | 984 | | { |
| | 12 | 985 | | if (valuesCount < 2) |
| | | 986 | | { |
| | 3 | 987 | | throw new InvalidDataException("A trace_obj statement must specify a file name."); |
| | | 988 | | } |
| | | 989 | | |
| | 9 | 990 | | var sb = new StringBuilder(); |
| | | 991 | | |
| | 9 | 992 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 993 | | |
| | 24 | 994 | | for (int i = 2; i < valuesCount; i++) |
| | | 995 | | { |
| | 3 | 996 | | sb.Append(' '); |
| | 3 | 997 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 998 | | } |
| | | 999 | | |
| | 9 | 1000 | | obj.TraceObjectFileName = sb.ToString(); |
| | 9 | 1001 | | break; |
| | | 1002 | | } |
| | | 1003 | | |
| | | 1004 | | case "ctech": |
| | 54 | 1005 | | context.CurveApproximationTechnique = ParseApproximationTechnique(value0, ref currentLine, ref v |
| | 27 | 1006 | | break; |
| | | 1007 | | |
| | | 1008 | | case "stech": |
| | 66 | 1009 | | context.SurfaceApproximationTechnique = ParseApproximationTechnique(value0, ref currentLine, ref |
| | 30 | 1010 | | break; |
| | | 1011 | | |
| | | 1012 | | case "bsp": |
| | | 1013 | | case "bzp": |
| | | 1014 | | case "cdc": |
| | | 1015 | | case "cdp": |
| | | 1016 | | case "res": |
| | 15 | 1017 | | throw new NotImplementedException(string.Concat(value0.ToString(), " statement have been replace |
| | | 1018 | | } |
| | | 1019 | | } |
| | | 1020 | | |
| | 351 | 1021 | | obj.HeaderText = string.Join("\n", lineReader.HeaderTextLines.ToArray()); |
| | | 1022 | | |
| | 351 | 1023 | | return obj; |
| | | 1024 | | } |
| | | 1025 | | |
| | | 1026 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | | 1027 | | private static ObjTriplet ParseTriplet(ObjFile obj, ReadOnlySpan<char> value) |
| | | 1028 | | { |
| | 258 | 1029 | | int valuesCount = 0; |
| | | 1030 | | |
| | 1134 | 1031 | | foreach (Range range in value.Split('/')) |
| | | 1032 | | { |
| | 309 | 1033 | | valuesCount++; |
| | | 1034 | | } |
| | | 1035 | | |
| | 258 | 1036 | | var values = value.Split('/'); |
| | | 1037 | | |
| | 258 | 1038 | | if (valuesCount > 3) |
| | | 1039 | | { |
| | 3 | 1040 | | throw new InvalidDataException("A triplet has too many values."); |
| | | 1041 | | } |
| | | 1042 | | |
| | 255 | 1043 | | values.MoveNext(); |
| | 255 | 1044 | | int v = value[values.Current].Length != 0 ? IntParse(value[values.Current]) : 0; |
| | | 1045 | | |
| | 255 | 1046 | | if (v == 0) |
| | | 1047 | | { |
| | 6 | 1048 | | throw new InvalidDataException("A triplet must specify a vertex index."); |
| | | 1049 | | } |
| | | 1050 | | |
| | 249 | 1051 | | if (v < 0) |
| | | 1052 | | { |
| | 6 | 1053 | | v = obj.Vertices.Count + v + 1; |
| | | 1054 | | } |
| | | 1055 | | |
| | 249 | 1056 | | if (v <= 0 || v > obj.Vertices.Count) |
| | | 1057 | | { |
| | 12 | 1058 | | throw new IndexOutOfRangeException(); |
| | | 1059 | | } |
| | | 1060 | | |
| | 237 | 1061 | | values.MoveNext(); |
| | 237 | 1062 | | int vt = valuesCount > 1 && value[values.Current].Length != 0 ? IntParse(value[values.Current]) : 0; |
| | | 1063 | | |
| | 237 | 1064 | | if (vt != 0) |
| | | 1065 | | { |
| | 12 | 1066 | | if (vt < 0) |
| | | 1067 | | { |
| | 3 | 1068 | | vt = obj.TextureVertices.Count + vt + 1; |
| | | 1069 | | } |
| | | 1070 | | |
| | 12 | 1071 | | if (vt <= 0 || vt > obj.TextureVertices.Count) |
| | | 1072 | | { |
| | 6 | 1073 | | throw new IndexOutOfRangeException(); |
| | | 1074 | | } |
| | | 1075 | | } |
| | | 1076 | | |
| | 231 | 1077 | | values.MoveNext(); |
| | 231 | 1078 | | int vn = valuesCount > 2 && value[values.Current].Length != 0 ? IntParse(value[values.Current]) : 0; |
| | | 1079 | | |
| | 231 | 1080 | | if (vn != 0) |
| | | 1081 | | { |
| | 12 | 1082 | | if (vn < 0) |
| | | 1083 | | { |
| | 3 | 1084 | | vn = obj.VertexNormals.Count + vn + 1; |
| | | 1085 | | } |
| | | 1086 | | |
| | 12 | 1087 | | if (vn <= 0 || vn > obj.VertexNormals.Count) |
| | | 1088 | | { |
| | 6 | 1089 | | throw new IndexOutOfRangeException(); |
| | | 1090 | | } |
| | | 1091 | | } |
| | | 1092 | | |
| | 225 | 1093 | | return new ObjTriplet(v, vt, vn); |
| | | 1094 | | } |
| | | 1095 | | |
| | | 1096 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | | 1097 | | private static ObjCurveIndex ParseCurveIndex(ObjFile obj, ref ReadOnlySpan<char> currentLine, ref SpanSplitEnume |
| | | 1098 | | { |
| | 87 | 1099 | | float start = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 87 | 1100 | | float end = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 87 | 1101 | | int curve2D = IntParse(GetNextValue(ref currentLine, ref values)); |
| | | 1102 | | |
| | 87 | 1103 | | if (curve2D == 0) |
| | | 1104 | | { |
| | 15 | 1105 | | throw new InvalidDataException("A curve index must specify an index."); |
| | | 1106 | | } |
| | | 1107 | | |
| | 72 | 1108 | | if (curve2D < 0) |
| | | 1109 | | { |
| | 15 | 1110 | | curve2D = obj.Curves2D.Count + curve2D + 1; |
| | | 1111 | | } |
| | | 1112 | | |
| | 72 | 1113 | | if (curve2D <= 0 || curve2D > obj.Curves2D.Count) |
| | | 1114 | | { |
| | 30 | 1115 | | throw new IndexOutOfRangeException(); |
| | | 1116 | | } |
| | | 1117 | | |
| | 42 | 1118 | | return new ObjCurveIndex(start, end, curve2D); |
| | | 1119 | | } |
| | | 1120 | | |
| | | 1121 | | private static void ParseCurveIndex(List<ObjCurveIndex> curves, ObjFile obj, ReadOnlySpan<char> value0, ref Read |
| | | 1122 | | { |
| | 81 | 1123 | | if (valuesCount < 4) |
| | | 1124 | | { |
| | 27 | 1125 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " statement must specify at least |
| | | 1126 | | } |
| | | 1127 | | |
| | 54 | 1128 | | if ((valuesCount - 1) % 3 != 0) |
| | | 1129 | | { |
| | 18 | 1130 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " statement has too many values.") |
| | | 1131 | | } |
| | | 1132 | | |
| | 108 | 1133 | | for (int i = 1; i < valuesCount; i += 3) |
| | | 1134 | | { |
| | 45 | 1135 | | curves.Add(ParseCurveIndex(obj, ref currentLine, ref values)); |
| | | 1136 | | } |
| | 9 | 1137 | | } |
| | | 1138 | | |
| | | 1139 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | | 1140 | | private static void ParseFreeFormType(ObjFileReaderContext context, ref ReadOnlySpan<char> value0, ref ReadOnlyS |
| | | 1141 | | { |
| | 45 | 1142 | | if (valuesCount < 2) |
| | | 1143 | | { |
| | 3 | 1144 | | throw new InvalidDataException("A cstype statement must specify a value."); |
| | | 1145 | | } |
| | | 1146 | | |
| | | 1147 | | string type; |
| | | 1148 | | |
| | 42 | 1149 | | if (valuesCount == 2) |
| | | 1150 | | { |
| | 18 | 1151 | | context.IsRationalForm = false; |
| | 18 | 1152 | | type = GetNextValue(ref currentLine, ref values).ToString(); |
| | | 1153 | | } |
| | 24 | 1154 | | else if (valuesCount == 3 && GetNextValue(ref currentLine, ref values).Equals("rat", StringComparison.Ordina |
| | | 1155 | | { |
| | 18 | 1156 | | context.IsRationalForm = true; |
| | 18 | 1157 | | type = GetNextValue(ref currentLine, ref values).ToString(); |
| | | 1158 | | } |
| | | 1159 | | else |
| | | 1160 | | { |
| | 6 | 1161 | | throw new InvalidDataException("A cstype statement has too many values."); |
| | | 1162 | | } |
| | | 1163 | | |
| | 36 | 1164 | | switch (type.ToLowerInvariant()) |
| | | 1165 | | { |
| | | 1166 | | case "bmatrix": |
| | 6 | 1167 | | context.FreeFormType = ObjFreeFormType.BasisMatrix; |
| | 6 | 1168 | | break; |
| | | 1169 | | |
| | | 1170 | | case "bezier": |
| | 6 | 1171 | | context.FreeFormType = ObjFreeFormType.Bezier; |
| | 6 | 1172 | | break; |
| | | 1173 | | |
| | | 1174 | | case "bspline": |
| | 6 | 1175 | | context.FreeFormType = ObjFreeFormType.BSpline; |
| | 6 | 1176 | | break; |
| | | 1177 | | |
| | | 1178 | | case "cardinal": |
| | 6 | 1179 | | context.FreeFormType = ObjFreeFormType.Cardinal; |
| | 6 | 1180 | | break; |
| | | 1181 | | |
| | | 1182 | | case "taylor": |
| | 6 | 1183 | | context.FreeFormType = ObjFreeFormType.Taylor; |
| | 6 | 1184 | | break; |
| | | 1185 | | |
| | | 1186 | | default: |
| | 6 | 1187 | | throw new InvalidDataException("A cstype statement has an unknown type."); |
| | | 1188 | | } |
| | | 1189 | | } |
| | | 1190 | | |
| | | 1191 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | | 1192 | | private static void ParseSurfaceConnection(ObjFile obj, ReadOnlySpan<char> value0, ref ReadOnlySpan<char> curren |
| | | 1193 | | { |
| | 66 | 1194 | | if (valuesCount < 9) |
| | | 1195 | | { |
| | 24 | 1196 | | throw new InvalidDataException("A con statement must specify 8 values."); |
| | | 1197 | | } |
| | | 1198 | | |
| | 42 | 1199 | | if (valuesCount != 9) |
| | | 1200 | | { |
| | 3 | 1201 | | throw new InvalidDataException("A con statement has too many values."); |
| | | 1202 | | } |
| | | 1203 | | |
| | 39 | 1204 | | int surface1 = IntParse(GetNextValue(ref currentLine, ref values)); |
| | | 1205 | | |
| | 39 | 1206 | | if (surface1 == 0) |
| | | 1207 | | { |
| | 3 | 1208 | | throw new InvalidDataException("A con statement must specify a surface index."); |
| | | 1209 | | } |
| | | 1210 | | |
| | 36 | 1211 | | if (surface1 < 0) |
| | | 1212 | | { |
| | 3 | 1213 | | surface1 = obj.Surfaces.Count + surface1 + 1; |
| | | 1214 | | } |
| | | 1215 | | |
| | 36 | 1216 | | if (surface1 <= 0 || surface1 > obj.Surfaces.Count) |
| | | 1217 | | { |
| | 6 | 1218 | | throw new IndexOutOfRangeException(); |
| | | 1219 | | } |
| | | 1220 | | |
| | 30 | 1221 | | var curve1 = ParseCurveIndex(obj, ref currentLine, ref values); |
| | | 1222 | | |
| | 21 | 1223 | | int surface2 = IntParse(GetNextValue(ref currentLine, ref values)); |
| | | 1224 | | |
| | 21 | 1225 | | if (surface2 == 0) |
| | | 1226 | | { |
| | 3 | 1227 | | throw new InvalidDataException("A con statement must specify a surface index."); |
| | | 1228 | | } |
| | | 1229 | | |
| | 18 | 1230 | | if (surface2 < 0) |
| | | 1231 | | { |
| | 3 | 1232 | | surface2 = obj.Surfaces.Count + surface2 + 1; |
| | | 1233 | | } |
| | | 1234 | | |
| | 18 | 1235 | | if (surface2 <= 0 || surface2 > obj.Surfaces.Count) |
| | | 1236 | | { |
| | 6 | 1237 | | throw new IndexOutOfRangeException(); |
| | | 1238 | | } |
| | | 1239 | | |
| | 12 | 1240 | | var curve2 = ParseCurveIndex(obj, ref currentLine, ref values); |
| | | 1241 | | |
| | 3 | 1242 | | var connection = new ObjSurfaceConnection |
| | 3 | 1243 | | { |
| | 3 | 1244 | | Surface1 = surface1, |
| | 3 | 1245 | | Curve2D1 = curve1, |
| | 3 | 1246 | | Surface2 = surface2, |
| | 3 | 1247 | | Curve2D2 = curve2 |
| | 3 | 1248 | | }; |
| | | 1249 | | |
| | 3 | 1250 | | obj.SurfaceConnections.Add(connection); |
| | 3 | 1251 | | } |
| | | 1252 | | |
| | | 1253 | | private static void ParseGroupName(ObjFileReaderContext context, ReadOnlySpan<char> value0, ref ReadOnlySpan<cha |
| | | 1254 | | { |
| | 87 | 1255 | | context.GroupNames.Clear(); |
| | | 1256 | | |
| | 87 | 1257 | | if (context.Settings.OnlyOneGroupNamePerLine) |
| | | 1258 | | { |
| | 6 | 1259 | | var sb = new StringBuilder(); |
| | | 1260 | | |
| | 6 | 1261 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 1262 | | |
| | 24 | 1263 | | for (int i = 2; i < valuesCount; i++) |
| | | 1264 | | { |
| | 6 | 1265 | | sb.Append(' '); |
| | 6 | 1266 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | | 1267 | | } |
| | | 1268 | | |
| | | 1269 | | |
| | 6 | 1270 | | var name = sb.ToString(); |
| | 6 | 1271 | | if (!string.Equals(name, "default", StringComparison.OrdinalIgnoreCase)) |
| | | 1272 | | { |
| | 6 | 1273 | | context.GroupNames.Add(name); |
| | | 1274 | | } |
| | | 1275 | | } |
| | | 1276 | | else |
| | | 1277 | | { |
| | 354 | 1278 | | for (int i = 1; i < valuesCount; i++) |
| | | 1279 | | { |
| | 96 | 1280 | | var name = GetNextValue(ref currentLine, ref values).ToString(); |
| | | 1281 | | |
| | 96 | 1282 | | if (!string.Equals(name, "default", StringComparison.OrdinalIgnoreCase)) |
| | | 1283 | | { |
| | 72 | 1284 | | context.GroupNames.Add(name); |
| | | 1285 | | } |
| | | 1286 | | } |
| | | 1287 | | } |
| | | 1288 | | |
| | 87 | 1289 | | context.GetCurrentGroups(); |
| | 87 | 1290 | | } |
| | | 1291 | | |
| | | 1292 | | private static ObjApproximationTechnique ParseApproximationTechnique(ReadOnlySpan<char> value0, ref ReadOnlySpan |
| | | 1293 | | { |
| | | 1294 | | ObjApproximationTechnique technique; |
| | | 1295 | | |
| | 120 | 1296 | | if (valuesCount < 2) |
| | | 1297 | | { |
| | 6 | 1298 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " statement must specify a techniq |
| | | 1299 | | } |
| | | 1300 | | |
| | 114 | 1301 | | string value1 = GetNextValue(ref currentLine, ref values).ToString().ToLowerInvariant(); |
| | | 1302 | | |
| | | 1303 | | switch (value1) |
| | | 1304 | | { |
| | | 1305 | | case "cparm": |
| | | 1306 | | { |
| | 9 | 1307 | | if (valuesCount < 3) |
| | | 1308 | | { |
| | 3 | 1309 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparm statement must |
| | | 1310 | | } |
| | | 1311 | | |
| | 6 | 1312 | | if (valuesCount != 3) |
| | | 1313 | | { |
| | 3 | 1314 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparm statement has |
| | | 1315 | | } |
| | | 1316 | | |
| | 3 | 1317 | | float res = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 1318 | | technique = new ObjConstantParametricSubdivisionTechnique(res); |
| | 3 | 1319 | | break; |
| | | 1320 | | } |
| | | 1321 | | |
| | | 1322 | | case "cparma": |
| | | 1323 | | { |
| | 12 | 1324 | | if (valuesCount < 4) |
| | | 1325 | | { |
| | 6 | 1326 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparma statement mus |
| | | 1327 | | } |
| | | 1328 | | |
| | 6 | 1329 | | if (valuesCount != 4) |
| | | 1330 | | { |
| | 3 | 1331 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparma statement has |
| | | 1332 | | } |
| | | 1333 | | |
| | 3 | 1334 | | float resU = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 1335 | | float resV = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 1336 | | technique = new ObjConstantParametricSubdivisionTechnique(resU, resV); |
| | 3 | 1337 | | break; |
| | | 1338 | | } |
| | | 1339 | | |
| | | 1340 | | case "cparmb": |
| | | 1341 | | { |
| | 9 | 1342 | | if (valuesCount < 3) |
| | | 1343 | | { |
| | 3 | 1344 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparmb statement mus |
| | | 1345 | | } |
| | | 1346 | | |
| | 6 | 1347 | | if (valuesCount != 3) |
| | | 1348 | | { |
| | 3 | 1349 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparmb statement has |
| | | 1350 | | } |
| | | 1351 | | |
| | 3 | 1352 | | float resU = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 3 | 1353 | | technique = new ObjConstantParametricSubdivisionTechnique(resU); |
| | 3 | 1354 | | break; |
| | | 1355 | | } |
| | | 1356 | | |
| | | 1357 | | case "cspace": |
| | | 1358 | | { |
| | 54 | 1359 | | if (valuesCount < 3) |
| | | 1360 | | { |
| | 6 | 1361 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cspace statement mus |
| | | 1362 | | } |
| | | 1363 | | |
| | 48 | 1364 | | if (valuesCount != 3) |
| | | 1365 | | { |
| | 6 | 1366 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cspace statement has |
| | | 1367 | | } |
| | | 1368 | | |
| | 42 | 1369 | | float length = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 42 | 1370 | | technique = new ObjConstantSpatialSubdivisionTechnique(length); |
| | 42 | 1371 | | break; |
| | | 1372 | | } |
| | | 1373 | | |
| | | 1374 | | case "curv": |
| | | 1375 | | { |
| | 24 | 1376 | | if (valuesCount < 4) |
| | | 1377 | | { |
| | 12 | 1378 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " curv statement must |
| | | 1379 | | } |
| | | 1380 | | |
| | 12 | 1381 | | if (valuesCount != 4) |
| | | 1382 | | { |
| | 6 | 1383 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " curv statement has t |
| | | 1384 | | } |
| | | 1385 | | |
| | 6 | 1386 | | float distance = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 6 | 1387 | | float angle = FloatParse(GetNextValue(ref currentLine, ref values)); |
| | 6 | 1388 | | technique = new ObjCurvatureDependentSubdivisionTechnique(distance, angle); |
| | 6 | 1389 | | break; |
| | | 1390 | | } |
| | | 1391 | | |
| | | 1392 | | default: |
| | 6 | 1393 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " statement contains an unknow |
| | | 1394 | | } |
| | | 1395 | | |
| | 57 | 1396 | | return technique; |
| | | 1397 | | } |
| | | 1398 | | } |
| | | 1399 | | } |
| | | 1400 | | |
| | | 1401 | | #endif |