| | 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 | | { |
| 6753 | 26 | | while (values.MoveNext()) |
| | 27 | | { |
| 6753 | 28 | | if (values.Current.Start.Value != values.Current.End.Value) |
| | 29 | | { |
| 6726 | 30 | | return; |
| | 31 | | } |
| | 32 | | } |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private static ReadOnlySpan<char> GetNextValue(ref ReadOnlySpan<char> currentLine, ref SpanSplitEnumerator value |
| | 36 | | { |
| 4587 | 37 | | MoveNextSkipEmpty(ref values); |
| 4587 | 38 | | return currentLine[values.Current]; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | private static float FloatParse(ReadOnlySpan<char> s) |
| | 42 | | { |
| 2973 | 43 | | return float.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | private static int IntParse(ReadOnlySpan<char> s) |
| | 47 | | { |
| 1104 | 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 | | { |
| 846 | 59 | | if (stream == null) |
| | 60 | | { |
| 3 | 61 | | throw new ArgumentNullException(nameof(stream)); |
| | 62 | | } |
| | 63 | |
|
| 843 | 64 | | var obj = new ObjFile(); |
| 843 | 65 | | var context = new ObjFileReaderContext(obj, settings); |
| 843 | 66 | | var lineReader = new LineReader9(); |
| | 67 | |
|
| 843 | 68 | | int valueBufferSize = 16; |
| 843 | 69 | | Span<char> valueBuffer = stackalloc char[valueBufferSize]; |
| | 70 | |
|
| 5460 | 71 | | foreach (var currentLineString in lineReader.Read9(stream)) |
| | 72 | | { |
| 2139 | 73 | | ReadOnlySpan<char> currentLine = currentLineString.Buffer.AsSpan()[..currentLineString.Length]; |
| | 74 | |
|
| 2139 | 75 | | int valuesCount = 0; |
| | 76 | |
|
| 19224 | 77 | | foreach (Range range in currentLine.SplitAny(LineReader9.LineSeparators)) |
| | 78 | | { |
| 7473 | 79 | | if (currentLine[range].Length == 0) |
| | 80 | | { |
| | 81 | | continue; |
| | 82 | | } |
| | 83 | |
|
| 7422 | 84 | | valuesCount++; |
| | 85 | | } |
| | 86 | |
|
| 2139 | 87 | | SpanSplitEnumerator values = currentLine.SplitAny(LineReader9.LineSeparators); |
| | 88 | |
|
| 2139 | 89 | | MoveNextSkipEmpty(ref values); |
| | 90 | |
|
| 2139 | 91 | | if (values.Current.End.Value - values.Current.Start.Value > valueBufferSize) |
| | 92 | | { |
| | 93 | | continue; |
| | 94 | | } |
| | 95 | |
|
| 2136 | 96 | | ReadOnlySpan<char> value0 = currentLine[values.Current]; |
| 2136 | 97 | | int value0Length = value0.ToLowerInvariant(valueBuffer); |
| | 98 | |
|
| | 99 | | //if (value0Length == -1) |
| | 100 | | //{ |
| | 101 | | // throw new InvalidDataException("the buffer is too small"); |
| | 102 | | //} |
| | 103 | |
|
| 2136 | 104 | | switch (valueBuffer[..value0Length]) |
| | 105 | | { |
| | 106 | | case "v": |
| | 107 | | { |
| 504 | 108 | | if (valuesCount < 4) |
| | 109 | | { |
| 9 | 110 | | throw new InvalidDataException("A v statement must specify at least 3 values."); |
| | 111 | | } |
| | 112 | |
|
| 495 | 113 | | float x = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 495 | 114 | | float y = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 495 | 115 | | float z = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 495 | 116 | | float w = 1.0f; |
| 495 | 117 | | bool hasColor = false; |
| 495 | 118 | | float r = 0.0f; |
| 495 | 119 | | float g = 0.0f; |
| 495 | 120 | | float b = 0.0f; |
| 495 | 121 | | float a = 1.0f; |
| | 122 | |
|
| 495 | 123 | | if (valuesCount == 4 || valuesCount == 5) |
| | 124 | | { |
| 483 | 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 | |
|
| 489 | 147 | | var v = new ObjVertex(); |
| 489 | 148 | | v.Position = new ObjVector4(x, y, z, w); |
| | 149 | |
|
| 489 | 150 | | if (hasColor) |
| | 151 | | { |
| 6 | 152 | | v.Color = new ObjVector4(r, g, b, a); |
| | 153 | | } |
| | 154 | |
|
| 489 | 155 | | obj.Vertices.Add(v); |
| 489 | 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 | | { |
| 102 | 351 | | if (valuesCount < 2) |
| | 352 | | { |
| 3 | 353 | | throw new InvalidDataException("A p statement must specify at least 1 value."); |
| | 354 | | } |
| | 355 | |
|
| 99 | 356 | | var point = new ObjPoint(); |
| | 357 | |
|
| 354 | 358 | | for (int i = 1; i < valuesCount; i++) |
| | 359 | | { |
| 105 | 360 | | point.Vertices.Add(ParseTriplet(obj, GetNextValue(ref currentLine, ref values))); |
| | 361 | | } |
| | 362 | |
|
| 72 | 363 | | context.ApplyAttributesToElement(point); |
| 72 | 364 | | context.ApplyAttributesToPolygonalElement(point); |
| | 365 | |
|
| 72 | 366 | | obj.Points.Add(point); |
| | 367 | |
|
| 288 | 368 | | foreach (var group in context.GetCurrentGroups()) |
| | 369 | | { |
| 72 | 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 | | { |
| 9 | 873 | | if (valuesCount < 2) |
| | 874 | | { |
| 3 | 875 | | throw new InvalidDataException("A mtllib statement must specify a file name."); |
| | 876 | | } |
| | 877 | |
|
| 6 | 878 | | var sb = new StringBuilder(); |
| | 879 | |
|
| 6 | 880 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | 881 | |
|
| 36 | 882 | | for (int i = 2; i < valuesCount; i++) |
| | 883 | | { |
| 12 | 884 | | sb.Append(' '); |
| 12 | 885 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | 886 | | } |
| | 887 | |
|
| 6 | 888 | | obj.MaterialLibraries.Add(sb.ToString()); |
| | 889 | |
|
| 6 | 890 | | break; |
| | 891 | | } |
| | 892 | |
|
| | 893 | | case "usemap": |
| | 894 | | { |
| 33 | 895 | | if (valuesCount < 2) |
| | 896 | | { |
| 3 | 897 | | throw new InvalidDataException("A usemap statement must specify a value."); |
| | 898 | | } |
| | 899 | |
|
| 30 | 900 | | if (valuesCount != 2) |
| | 901 | | { |
| 3 | 902 | | throw new InvalidDataException("A usemap statement has too many values."); |
| | 903 | | } |
| | 904 | |
|
| 27 | 905 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | 906 | |
|
| 27 | 907 | | if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | 908 | | { |
| 3 | 909 | | context.MapName = null; |
| | 910 | | } |
| | 911 | | else |
| | 912 | | { |
| 24 | 913 | | context.MapName = value1.ToString(); |
| | 914 | | } |
| | 915 | |
|
| 24 | 916 | | break; |
| | 917 | | } |
| | 918 | |
|
| | 919 | | case "usemtl": |
| | 920 | | { |
| 33 | 921 | | if (valuesCount < 2) |
| | 922 | | { |
| 3 | 923 | | throw new InvalidDataException("A usemtl statement must specify a value."); |
| | 924 | | } |
| | 925 | |
|
| 30 | 926 | | var value1 = GetNextValue(ref currentLine, ref values); |
| | 927 | |
|
| 30 | 928 | | if (value1.Equals("off", StringComparison.OrdinalIgnoreCase)) |
| | 929 | | { |
| 6 | 930 | | if (valuesCount != 2) |
| | 931 | | { |
| 3 | 932 | | throw new InvalidDataException("A usemtl statement has too many values."); |
| | 933 | | } |
| | 934 | |
|
| 3 | 935 | | context.MaterialName = null; |
| | 936 | | } |
| | 937 | | else |
| | 938 | | { |
| 24 | 939 | | var sb = new StringBuilder(); |
| | 940 | |
|
| 24 | 941 | | sb.Append(value1); |
| | 942 | |
|
| 60 | 943 | | for (int i = 2; i < valuesCount; i++) |
| | 944 | | { |
| 6 | 945 | | sb.Append(' '); |
| 6 | 946 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | 947 | | } |
| | 948 | |
|
| 24 | 949 | | context.MaterialName = sb.ToString(); |
| | 950 | | } |
| | 951 | |
|
| 24 | 952 | | break; |
| | 953 | | } |
| | 954 | |
|
| | 955 | | case "shadow_obj": |
| | 956 | | { |
| 18 | 957 | | if (valuesCount < 2) |
| | 958 | | { |
| 3 | 959 | | throw new InvalidDataException("A shadow_obj statement must specify a file name."); |
| | 960 | | } |
| | 961 | |
|
| 15 | 962 | | var sb = new StringBuilder(); |
| | 963 | |
|
| 15 | 964 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | 965 | |
|
| 36 | 966 | | for (int i = 2; i < valuesCount; i++) |
| | 967 | | { |
| 3 | 968 | | sb.Append(' '); |
| 3 | 969 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | 970 | | } |
| | 971 | |
|
| 15 | 972 | | obj.ShadowObjectFileName = sb.ToString(); |
| 15 | 973 | | break; |
| | 974 | | } |
| | 975 | |
|
| | 976 | | case "trace_obj": |
| | 977 | | { |
| 12 | 978 | | if (valuesCount < 2) |
| | 979 | | { |
| 3 | 980 | | throw new InvalidDataException("A trace_obj statement must specify a file name."); |
| | 981 | | } |
| | 982 | |
|
| 9 | 983 | | var sb = new StringBuilder(); |
| | 984 | |
|
| 9 | 985 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | 986 | |
|
| 24 | 987 | | for (int i = 2; i < valuesCount; i++) |
| | 988 | | { |
| 3 | 989 | | sb.Append(' '); |
| 3 | 990 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | 991 | | } |
| | 992 | |
|
| 9 | 993 | | obj.TraceObjectFileName = sb.ToString(); |
| 9 | 994 | | break; |
| | 995 | | } |
| | 996 | |
|
| | 997 | | case "ctech": |
| 54 | 998 | | context.CurveApproximationTechnique = ParseApproximationTechnique(value0, ref currentLine, ref v |
| 27 | 999 | | break; |
| | 1000 | |
|
| | 1001 | | case "stech": |
| 66 | 1002 | | context.SurfaceApproximationTechnique = ParseApproximationTechnique(value0, ref currentLine, ref |
| 30 | 1003 | | break; |
| | 1004 | |
|
| | 1005 | | case "bsp": |
| | 1006 | | case "bzp": |
| | 1007 | | case "cdc": |
| | 1008 | | case "cdp": |
| | 1009 | | case "res": |
| 15 | 1010 | | throw new NotImplementedException(string.Concat(value0.ToString(), " statement have been replace |
| | 1011 | | } |
| | 1012 | | } |
| | 1013 | |
|
| 339 | 1014 | | obj.HeaderText = string.Join("\n", lineReader.HeaderTextLines.ToArray()); |
| | 1015 | |
|
| 339 | 1016 | | return obj; |
| | 1017 | | } |
| | 1018 | |
|
| | 1019 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | 1020 | | private static ObjTriplet ParseTriplet(ObjFile obj, ReadOnlySpan<char> value) |
| | 1021 | | { |
| 255 | 1022 | | int valuesCount = 0; |
| | 1023 | |
|
| 1122 | 1024 | | foreach (Range range in value.Split('/')) |
| | 1025 | | { |
| 306 | 1026 | | valuesCount++; |
| | 1027 | | } |
| | 1028 | |
|
| 255 | 1029 | | var values = value.Split('/'); |
| | 1030 | |
|
| 255 | 1031 | | if (valuesCount > 3) |
| | 1032 | | { |
| 3 | 1033 | | throw new InvalidDataException("A triplet has too many values."); |
| | 1034 | | } |
| | 1035 | |
|
| 252 | 1036 | | values.MoveNext(); |
| 252 | 1037 | | int v = value[values.Current].Length != 0 ? IntParse(value[values.Current]) : 0; |
| | 1038 | |
|
| 252 | 1039 | | if (v == 0) |
| | 1040 | | { |
| 6 | 1041 | | throw new InvalidDataException("A triplet must specify a vertex index."); |
| | 1042 | | } |
| | 1043 | |
|
| 246 | 1044 | | if (v < 0) |
| | 1045 | | { |
| 6 | 1046 | | v = obj.Vertices.Count + v + 1; |
| | 1047 | | } |
| | 1048 | |
|
| 246 | 1049 | | if (v <= 0 || v > obj.Vertices.Count) |
| | 1050 | | { |
| 12 | 1051 | | throw new IndexOutOfRangeException(); |
| | 1052 | | } |
| | 1053 | |
|
| 234 | 1054 | | values.MoveNext(); |
| 234 | 1055 | | int vt = valuesCount > 1 && value[values.Current].Length != 0 ? IntParse(value[values.Current]) : 0; |
| | 1056 | |
|
| 234 | 1057 | | if (vt != 0) |
| | 1058 | | { |
| 12 | 1059 | | if (vt < 0) |
| | 1060 | | { |
| 3 | 1061 | | vt = obj.TextureVertices.Count + vt + 1; |
| | 1062 | | } |
| | 1063 | |
|
| 12 | 1064 | | if (vt <= 0 || vt > obj.TextureVertices.Count) |
| | 1065 | | { |
| 6 | 1066 | | throw new IndexOutOfRangeException(); |
| | 1067 | | } |
| | 1068 | | } |
| | 1069 | |
|
| 228 | 1070 | | values.MoveNext(); |
| 228 | 1071 | | int vn = valuesCount > 2 && value[values.Current].Length != 0 ? IntParse(value[values.Current]) : 0; |
| | 1072 | |
|
| 228 | 1073 | | if (vn != 0) |
| | 1074 | | { |
| 12 | 1075 | | if (vn < 0) |
| | 1076 | | { |
| 3 | 1077 | | vn = obj.VertexNormals.Count + vn + 1; |
| | 1078 | | } |
| | 1079 | |
|
| 12 | 1080 | | if (vn <= 0 || vn > obj.VertexNormals.Count) |
| | 1081 | | { |
| 6 | 1082 | | throw new IndexOutOfRangeException(); |
| | 1083 | | } |
| | 1084 | | } |
| | 1085 | |
|
| 222 | 1086 | | return new ObjTriplet(v, vt, vn); |
| | 1087 | | } |
| | 1088 | |
|
| | 1089 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | 1090 | | private static ObjCurveIndex ParseCurveIndex(ObjFile obj, ref ReadOnlySpan<char> currentLine, ref SpanSplitEnume |
| | 1091 | | { |
| 87 | 1092 | | float start = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 87 | 1093 | | float end = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 87 | 1094 | | int curve2D = IntParse(GetNextValue(ref currentLine, ref values)); |
| | 1095 | |
|
| 87 | 1096 | | if (curve2D == 0) |
| | 1097 | | { |
| 15 | 1098 | | throw new InvalidDataException("A curve index must specify an index."); |
| | 1099 | | } |
| | 1100 | |
|
| 72 | 1101 | | if (curve2D < 0) |
| | 1102 | | { |
| 15 | 1103 | | curve2D = obj.Curves2D.Count + curve2D + 1; |
| | 1104 | | } |
| | 1105 | |
|
| 72 | 1106 | | if (curve2D <= 0 || curve2D > obj.Curves2D.Count) |
| | 1107 | | { |
| 30 | 1108 | | throw new IndexOutOfRangeException(); |
| | 1109 | | } |
| | 1110 | |
|
| 42 | 1111 | | return new ObjCurveIndex(start, end, curve2D); |
| | 1112 | | } |
| | 1113 | |
|
| | 1114 | | private static void ParseCurveIndex(List<ObjCurveIndex> curves, ObjFile obj, ReadOnlySpan<char> value0, ref Read |
| | 1115 | | { |
| 81 | 1116 | | if (valuesCount < 4) |
| | 1117 | | { |
| 27 | 1118 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " statement must specify at least |
| | 1119 | | } |
| | 1120 | |
|
| 54 | 1121 | | if ((valuesCount - 1) % 3 != 0) |
| | 1122 | | { |
| 18 | 1123 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " statement has too many values.") |
| | 1124 | | } |
| | 1125 | |
|
| 108 | 1126 | | for (int i = 1; i < valuesCount; i += 3) |
| | 1127 | | { |
| 45 | 1128 | | curves.Add(ParseCurveIndex(obj, ref currentLine, ref values)); |
| | 1129 | | } |
| 9 | 1130 | | } |
| | 1131 | |
|
| | 1132 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | 1133 | | private static void ParseFreeFormType(ObjFileReaderContext context, ref ReadOnlySpan<char> value0, ref ReadOnlyS |
| | 1134 | | { |
| 45 | 1135 | | if (valuesCount < 2) |
| | 1136 | | { |
| 3 | 1137 | | throw new InvalidDataException("A cstype statement must specify a value."); |
| | 1138 | | } |
| | 1139 | |
|
| | 1140 | | string type; |
| | 1141 | |
|
| 42 | 1142 | | if (valuesCount == 2) |
| | 1143 | | { |
| 18 | 1144 | | context.IsRationalForm = false; |
| 18 | 1145 | | type = GetNextValue(ref currentLine, ref values).ToString(); |
| | 1146 | | } |
| 24 | 1147 | | else if (valuesCount == 3 && GetNextValue(ref currentLine, ref values).Equals("rat", StringComparison.Ordina |
| | 1148 | | { |
| 18 | 1149 | | context.IsRationalForm = true; |
| 18 | 1150 | | type = GetNextValue(ref currentLine, ref values).ToString(); |
| | 1151 | | } |
| | 1152 | | else |
| | 1153 | | { |
| 6 | 1154 | | throw new InvalidDataException("A cstype statement has too many values."); |
| | 1155 | | } |
| | 1156 | |
|
| 36 | 1157 | | switch (type.ToLowerInvariant()) |
| | 1158 | | { |
| | 1159 | | case "bmatrix": |
| 6 | 1160 | | context.FreeFormType = ObjFreeFormType.BasisMatrix; |
| 6 | 1161 | | break; |
| | 1162 | |
|
| | 1163 | | case "bezier": |
| 6 | 1164 | | context.FreeFormType = ObjFreeFormType.Bezier; |
| 6 | 1165 | | break; |
| | 1166 | |
|
| | 1167 | | case "bspline": |
| 6 | 1168 | | context.FreeFormType = ObjFreeFormType.BSpline; |
| 6 | 1169 | | break; |
| | 1170 | |
|
| | 1171 | | case "cardinal": |
| 6 | 1172 | | context.FreeFormType = ObjFreeFormType.Cardinal; |
| 6 | 1173 | | break; |
| | 1174 | |
|
| | 1175 | | case "taylor": |
| 6 | 1176 | | context.FreeFormType = ObjFreeFormType.Taylor; |
| 6 | 1177 | | break; |
| | 1178 | |
|
| | 1179 | | default: |
| 6 | 1180 | | throw new InvalidDataException("A cstype statement has an unknown type."); |
| | 1181 | | } |
| | 1182 | | } |
| | 1183 | |
|
| | 1184 | | [SuppressMessage("Globalization", "CA1303:Ne pas passer de littéraux en paramètres localisés", Justification = " |
| | 1185 | | private static void ParseSurfaceConnection(ObjFile obj, ReadOnlySpan<char> value0, ref ReadOnlySpan<char> curren |
| | 1186 | | { |
| 66 | 1187 | | if (valuesCount < 9) |
| | 1188 | | { |
| 24 | 1189 | | throw new InvalidDataException("A con statement must specify 8 values."); |
| | 1190 | | } |
| | 1191 | |
|
| 42 | 1192 | | if (valuesCount != 9) |
| | 1193 | | { |
| 3 | 1194 | | throw new InvalidDataException("A con statement has too many values."); |
| | 1195 | | } |
| | 1196 | |
|
| 39 | 1197 | | int surface1 = IntParse(GetNextValue(ref currentLine, ref values)); |
| | 1198 | |
|
| 39 | 1199 | | if (surface1 == 0) |
| | 1200 | | { |
| 3 | 1201 | | throw new InvalidDataException("A con statement must specify a surface index."); |
| | 1202 | | } |
| | 1203 | |
|
| 36 | 1204 | | if (surface1 < 0) |
| | 1205 | | { |
| 3 | 1206 | | surface1 = obj.Surfaces.Count + surface1 + 1; |
| | 1207 | | } |
| | 1208 | |
|
| 36 | 1209 | | if (surface1 <= 0 || surface1 > obj.Surfaces.Count) |
| | 1210 | | { |
| 6 | 1211 | | throw new IndexOutOfRangeException(); |
| | 1212 | | } |
| | 1213 | |
|
| 30 | 1214 | | var curve1 = ParseCurveIndex(obj, ref currentLine, ref values); |
| | 1215 | |
|
| 21 | 1216 | | int surface2 = IntParse(GetNextValue(ref currentLine, ref values)); |
| | 1217 | |
|
| 21 | 1218 | | if (surface2 == 0) |
| | 1219 | | { |
| 3 | 1220 | | throw new InvalidDataException("A con statement must specify a surface index."); |
| | 1221 | | } |
| | 1222 | |
|
| 18 | 1223 | | if (surface2 < 0) |
| | 1224 | | { |
| 3 | 1225 | | surface2 = obj.Surfaces.Count + surface2 + 1; |
| | 1226 | | } |
| | 1227 | |
|
| 18 | 1228 | | if (surface2 <= 0 || surface2 > obj.Surfaces.Count) |
| | 1229 | | { |
| 6 | 1230 | | throw new IndexOutOfRangeException(); |
| | 1231 | | } |
| | 1232 | |
|
| 12 | 1233 | | var curve2 = ParseCurveIndex(obj, ref currentLine, ref values); |
| | 1234 | |
|
| 3 | 1235 | | var connection = new ObjSurfaceConnection |
| 3 | 1236 | | { |
| 3 | 1237 | | Surface1 = surface1, |
| 3 | 1238 | | Curve2D1 = curve1, |
| 3 | 1239 | | Surface2 = surface2, |
| 3 | 1240 | | Curve2D2 = curve2 |
| 3 | 1241 | | }; |
| | 1242 | |
|
| 3 | 1243 | | obj.SurfaceConnections.Add(connection); |
| 3 | 1244 | | } |
| | 1245 | |
|
| | 1246 | | private static void ParseGroupName(ObjFileReaderContext context, ReadOnlySpan<char> value0, ref ReadOnlySpan<cha |
| | 1247 | | { |
| 87 | 1248 | | context.GroupNames.Clear(); |
| | 1249 | |
|
| 87 | 1250 | | if (context.Settings.OnlyOneGroupNamePerLine) |
| | 1251 | | { |
| 6 | 1252 | | var sb = new StringBuilder(); |
| | 1253 | |
|
| 6 | 1254 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | 1255 | |
|
| 24 | 1256 | | for (int i = 2; i < valuesCount; i++) |
| | 1257 | | { |
| 6 | 1258 | | sb.Append(' '); |
| 6 | 1259 | | sb.Append(GetNextValue(ref currentLine, ref values)); |
| | 1260 | | } |
| | 1261 | |
|
| | 1262 | |
|
| 6 | 1263 | | var name = sb.ToString(); |
| 6 | 1264 | | if (!string.Equals(name, "default", StringComparison.OrdinalIgnoreCase)) |
| | 1265 | | { |
| 6 | 1266 | | context.GroupNames.Add(name); |
| | 1267 | | } |
| | 1268 | | } |
| | 1269 | | else |
| | 1270 | | { |
| 354 | 1271 | | for (int i = 1; i < valuesCount; i++) |
| | 1272 | | { |
| 96 | 1273 | | var name = GetNextValue(ref currentLine, ref values).ToString(); |
| | 1274 | |
|
| 96 | 1275 | | if (!string.Equals(name, "default", StringComparison.OrdinalIgnoreCase)) |
| | 1276 | | { |
| 72 | 1277 | | context.GroupNames.Add(name); |
| | 1278 | | } |
| | 1279 | | } |
| | 1280 | | } |
| | 1281 | |
|
| 87 | 1282 | | context.GetCurrentGroups(); |
| 87 | 1283 | | } |
| | 1284 | |
|
| | 1285 | | private static ObjApproximationTechnique ParseApproximationTechnique(ReadOnlySpan<char> value0, ref ReadOnlySpan |
| | 1286 | | { |
| | 1287 | | ObjApproximationTechnique technique; |
| | 1288 | |
|
| 120 | 1289 | | if (valuesCount < 2) |
| | 1290 | | { |
| 6 | 1291 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " statement must specify a techniq |
| | 1292 | | } |
| | 1293 | |
|
| 114 | 1294 | | string value1 = GetNextValue(ref currentLine, ref values).ToString().ToLowerInvariant(); |
| | 1295 | |
|
| | 1296 | | switch (value1) |
| | 1297 | | { |
| | 1298 | | case "cparm": |
| | 1299 | | { |
| 9 | 1300 | | if (valuesCount < 3) |
| | 1301 | | { |
| 3 | 1302 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparm statement must |
| | 1303 | | } |
| | 1304 | |
|
| 6 | 1305 | | if (valuesCount != 3) |
| | 1306 | | { |
| 3 | 1307 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparm statement has |
| | 1308 | | } |
| | 1309 | |
|
| 3 | 1310 | | float res = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 3 | 1311 | | technique = new ObjConstantParametricSubdivisionTechnique(res); |
| 3 | 1312 | | break; |
| | 1313 | | } |
| | 1314 | |
|
| | 1315 | | case "cparma": |
| | 1316 | | { |
| 12 | 1317 | | if (valuesCount < 4) |
| | 1318 | | { |
| 6 | 1319 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparma statement mus |
| | 1320 | | } |
| | 1321 | |
|
| 6 | 1322 | | if (valuesCount != 4) |
| | 1323 | | { |
| 3 | 1324 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparma statement has |
| | 1325 | | } |
| | 1326 | |
|
| 3 | 1327 | | float resU = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 3 | 1328 | | float resV = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 3 | 1329 | | technique = new ObjConstantParametricSubdivisionTechnique(resU, resV); |
| 3 | 1330 | | break; |
| | 1331 | | } |
| | 1332 | |
|
| | 1333 | | case "cparmb": |
| | 1334 | | { |
| 9 | 1335 | | if (valuesCount < 3) |
| | 1336 | | { |
| 3 | 1337 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparmb statement mus |
| | 1338 | | } |
| | 1339 | |
|
| 6 | 1340 | | if (valuesCount != 3) |
| | 1341 | | { |
| 3 | 1342 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cparmb statement has |
| | 1343 | | } |
| | 1344 | |
|
| 3 | 1345 | | float resU = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 3 | 1346 | | technique = new ObjConstantParametricSubdivisionTechnique(resU); |
| 3 | 1347 | | break; |
| | 1348 | | } |
| | 1349 | |
|
| | 1350 | | case "cspace": |
| | 1351 | | { |
| 54 | 1352 | | if (valuesCount < 3) |
| | 1353 | | { |
| 6 | 1354 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cspace statement mus |
| | 1355 | | } |
| | 1356 | |
|
| 48 | 1357 | | if (valuesCount != 3) |
| | 1358 | | { |
| 6 | 1359 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " cspace statement has |
| | 1360 | | } |
| | 1361 | |
|
| 42 | 1362 | | float length = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 42 | 1363 | | technique = new ObjConstantSpatialSubdivisionTechnique(length); |
| 42 | 1364 | | break; |
| | 1365 | | } |
| | 1366 | |
|
| | 1367 | | case "curv": |
| | 1368 | | { |
| 24 | 1369 | | if (valuesCount < 4) |
| | 1370 | | { |
| 12 | 1371 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " curv statement must |
| | 1372 | | } |
| | 1373 | |
|
| 12 | 1374 | | if (valuesCount != 4) |
| | 1375 | | { |
| 6 | 1376 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " curv statement has t |
| | 1377 | | } |
| | 1378 | |
|
| 6 | 1379 | | float distance = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 6 | 1380 | | float angle = FloatParse(GetNextValue(ref currentLine, ref values)); |
| 6 | 1381 | | technique = new ObjCurvatureDependentSubdivisionTechnique(distance, angle); |
| 6 | 1382 | | break; |
| | 1383 | | } |
| | 1384 | |
|
| | 1385 | | default: |
| 6 | 1386 | | throw new InvalidDataException(string.Concat("A ", value0.ToString(), " statement contains an unknow |
| | 1387 | | } |
| | 1388 | |
|
| 57 | 1389 | | return technique; |
| | 1390 | | } |
| | 1391 | | } |
| | 1392 | | } |
| | 1393 | |
|
| | 1394 | | #endif |
| | 1395 | |
|