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