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