| | 1 | | // <copyright file="ObjFileWriterContext.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.Globalization; |
| | 9 | |
|
| | 10 | | namespace JeremyAnsel.Media.WavefrontObj |
| | 11 | | { |
| | 12 | | internal class ObjFileWriterContext |
| | 13 | | { |
| | 14 | | private ObjFile obj; |
| | 15 | |
|
| 216 | 16 | | public ObjFileWriterContext(ObjFile obj) |
| | 17 | | { |
| 216 | 18 | | this.obj = obj; |
| 216 | 19 | | this.GroupNames = string.Empty; |
| 216 | 20 | | } |
| | 21 | |
|
| 441 | 22 | | public string GroupNames { get; private set; } |
| | 23 | |
|
| 264 | 24 | | public string? ObjectName { get; private set; } |
| | 25 | |
|
| 237 | 26 | | public int LevelOfDetail { get; private set; } |
| | 27 | |
|
| 282 | 28 | | public string? MapName { get; private set; } |
| | 29 | |
|
| 282 | 30 | | public string? MaterialName { get; private set; } |
| | 31 | |
|
| 147 | 32 | | public long SmoothingGroupNumber { get; private set; } |
| | 33 | |
|
| 117 | 34 | | public bool IsBevelInterpolationEnabled { get; private set; } |
| | 35 | |
|
| 117 | 36 | | public bool IsColorInterpolationEnabled { get; private set; } |
| | 37 | |
|
| 117 | 38 | | public bool IsDissolveInterpolationEnabled { get; private set; } |
| | 39 | |
|
| 162 | 40 | | public int MergingGroupNumber { get; private set; } |
| | 41 | |
|
| | 42 | | public void WriteGroupNames<T>(StreamWriter stream, T element, Func<ObjGroup, List<T>> func) |
| | 43 | | where T : ObjElement |
| | 44 | | { |
| 195 | 45 | | string groupNames = GetGroupNames(element, func); |
| | 46 | |
|
| 195 | 47 | | if (groupNames != this.GroupNames) |
| | 48 | | { |
| 30 | 49 | | this.GroupNames = groupNames; |
| | 50 | |
|
| 30 | 51 | | if (string.IsNullOrEmpty(groupNames)) |
| | 52 | | { |
| 3 | 53 | | stream.WriteLine("g default"); |
| | 54 | | } |
| | 55 | | else |
| | 56 | | { |
| 27 | 57 | | stream.WriteLine("g {0}", groupNames); |
| | 58 | | } |
| | 59 | | } |
| 192 | 60 | | } |
| | 61 | |
|
| | 62 | | public void WriteAttributesOfElement(StreamWriter stream, ObjElement element) |
| | 63 | | { |
| 195 | 64 | | if (element.ObjectName != this.ObjectName) |
| | 65 | | { |
| 24 | 66 | | this.ObjectName = element.ObjectName; |
| | 67 | |
|
| 24 | 68 | | if (string.IsNullOrEmpty(this.ObjectName)) |
| | 69 | | { |
| 3 | 70 | | stream.WriteLine("o"); |
| | 71 | | } |
| | 72 | | else |
| | 73 | | { |
| 21 | 74 | | stream.WriteLine("o {0}", this.ObjectName); |
| | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| 195 | 78 | | if (element.LevelOfDetail != this.LevelOfDetail) |
| | 79 | | { |
| 21 | 80 | | this.LevelOfDetail = element.LevelOfDetail; |
| | 81 | |
|
| 21 | 82 | | stream.WriteLine("lod {0}", this.LevelOfDetail); |
| | 83 | | } |
| | 84 | |
|
| 195 | 85 | | if (element.MapName != this.MapName) |
| | 86 | | { |
| 30 | 87 | | this.MapName = element.MapName; |
| | 88 | |
|
| 30 | 89 | | if (string.IsNullOrEmpty(this.MapName)) |
| | 90 | | { |
| 3 | 91 | | stream.WriteLine("usemap off"); |
| | 92 | | } |
| | 93 | | else |
| | 94 | | { |
| 27 | 95 | | stream.WriteLine("usemap {0}", this.MapName); |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| 195 | 99 | | if (element.MaterialName != this.MaterialName) |
| | 100 | | { |
| 30 | 101 | | this.MaterialName = element.MaterialName; |
| | 102 | |
|
| 30 | 103 | | if (string.IsNullOrEmpty(this.MaterialName)) |
| | 104 | | { |
| 3 | 105 | | stream.WriteLine("usemtl off"); |
| | 106 | | } |
| | 107 | | else |
| | 108 | | { |
| 27 | 109 | | stream.WriteLine("usemtl {0}", this.MaterialName); |
| | 110 | | } |
| | 111 | | } |
| 192 | 112 | | } |
| | 113 | |
|
| | 114 | | public void WriteAttributesOfPolygonalElement(StreamWriter stream, ObjPolygonalElement element) |
| | 115 | | { |
| 87 | 116 | | if (element.SmoothingGroupNumber != this.SmoothingGroupNumber) |
| | 117 | | { |
| 21 | 118 | | this.SmoothingGroupNumber = element.SmoothingGroupNumber; |
| | 119 | |
|
| 21 | 120 | | if (this.SmoothingGroupNumber == 0) |
| | 121 | | { |
| 3 | 122 | | stream.WriteLine("s off"); |
| | 123 | | } |
| | 124 | | else |
| | 125 | | { |
| 18 | 126 | | stream.WriteLine("s {0}", this.SmoothingGroupNumber); |
| | 127 | | } |
| | 128 | | } |
| | 129 | |
|
| 87 | 130 | | if (element.IsBevelInterpolationEnabled != this.IsBevelInterpolationEnabled) |
| | 131 | | { |
| 15 | 132 | | this.IsBevelInterpolationEnabled = element.IsBevelInterpolationEnabled; |
| | 133 | |
|
| 15 | 134 | | if (this.IsBevelInterpolationEnabled) |
| | 135 | | { |
| 12 | 136 | | stream.WriteLine("bevel on"); |
| | 137 | | } |
| | 138 | | else |
| | 139 | | { |
| 3 | 140 | | stream.WriteLine("bevel off"); |
| | 141 | | } |
| | 142 | | } |
| | 143 | |
|
| 87 | 144 | | if (element.IsColorInterpolationEnabled != this.IsColorInterpolationEnabled) |
| | 145 | | { |
| 15 | 146 | | this.IsColorInterpolationEnabled = element.IsColorInterpolationEnabled; |
| | 147 | |
|
| 15 | 148 | | if (this.IsColorInterpolationEnabled) |
| | 149 | | { |
| 12 | 150 | | stream.WriteLine("c_interp on"); |
| | 151 | | } |
| | 152 | | else |
| | 153 | | { |
| 3 | 154 | | stream.WriteLine("c_interp off"); |
| | 155 | | } |
| | 156 | | } |
| | 157 | |
|
| 87 | 158 | | if (element.IsDissolveInterpolationEnabled != this.IsDissolveInterpolationEnabled) |
| | 159 | | { |
| 15 | 160 | | this.IsDissolveInterpolationEnabled = element.IsDissolveInterpolationEnabled; |
| | 161 | |
|
| 15 | 162 | | if (this.IsDissolveInterpolationEnabled) |
| | 163 | | { |
| 12 | 164 | | stream.WriteLine("d_interp on"); |
| | 165 | | } |
| | 166 | | else |
| | 167 | | { |
| 3 | 168 | | stream.WriteLine("d_interp off"); |
| | 169 | | } |
| | 170 | | } |
| 75 | 171 | | } |
| | 172 | |
|
| | 173 | | public void WriteAttributesOfFreeFormElement(StreamWriter stream, ObjFreeFormElement element) |
| | 174 | | { |
| 108 | 175 | | if (element.MergingGroupNumber != this.MergingGroupNumber) |
| | 176 | | { |
| 15 | 177 | | this.MergingGroupNumber = element.MergingGroupNumber; |
| | 178 | |
|
| 15 | 179 | | if (this.MergingGroupNumber == 0) |
| | 180 | | { |
| 3 | 181 | | stream.WriteLine("mg off"); |
| | 182 | | } |
| | 183 | | else |
| | 184 | | { |
| 12 | 185 | | float res = obj.MergingGroupResolutions[this.MergingGroupNumber]; |
| 12 | 186 | | stream.WriteLine("mg {0} {1}", this.MergingGroupNumber, res.ToString("F6", CultureInfo.InvariantCult |
| | 187 | | } |
| | 188 | | } |
| | 189 | |
|
| 108 | 190 | | switch (element.FreeFormType) |
| | 191 | | { |
| | 192 | | case ObjFreeFormType.BasisMatrix: |
| 81 | 193 | | stream.Write("cstype"); |
| | 194 | |
|
| 81 | 195 | | if (element.IsRationalForm) |
| | 196 | | { |
| 78 | 197 | | stream.Write(" rat"); |
| | 198 | | } |
| | 199 | |
|
| 81 | 200 | | stream.WriteLine(" bmatrix"); |
| 81 | 201 | | break; |
| | 202 | |
|
| | 203 | | case ObjFreeFormType.Bezier: |
| 6 | 204 | | stream.Write("cstype"); |
| | 205 | |
|
| 6 | 206 | | if (element.IsRationalForm) |
| | 207 | | { |
| 3 | 208 | | stream.Write(" rat"); |
| | 209 | | } |
| | 210 | |
|
| 6 | 211 | | stream.WriteLine(" bezier"); |
| 6 | 212 | | break; |
| | 213 | |
|
| | 214 | | case ObjFreeFormType.BSpline: |
| 6 | 215 | | stream.Write("cstype"); |
| | 216 | |
|
| 6 | 217 | | if (element.IsRationalForm) |
| | 218 | | { |
| 3 | 219 | | stream.Write(" rat"); |
| | 220 | | } |
| | 221 | |
|
| 6 | 222 | | stream.WriteLine(" bspline"); |
| 6 | 223 | | break; |
| | 224 | |
|
| | 225 | | case ObjFreeFormType.Cardinal: |
| 6 | 226 | | stream.Write("cstype"); |
| | 227 | |
|
| 6 | 228 | | if (element.IsRationalForm) |
| | 229 | | { |
| 3 | 230 | | stream.Write(" rat"); |
| | 231 | | } |
| | 232 | |
|
| 6 | 233 | | stream.WriteLine(" cardinal"); |
| 6 | 234 | | break; |
| | 235 | |
|
| | 236 | | case ObjFreeFormType.Taylor: |
| 6 | 237 | | stream.Write("cstype"); |
| | 238 | |
|
| 6 | 239 | | if (element.IsRationalForm) |
| | 240 | | { |
| 3 | 241 | | stream.Write(" rat"); |
| | 242 | | } |
| | 243 | |
|
| 6 | 244 | | stream.WriteLine(" taylor"); |
| | 245 | | break; |
| | 246 | | } |
| | 247 | |
|
| 108 | 248 | | if (element.DegreeV == 0) |
| | 249 | | { |
| 3 | 250 | | stream.WriteLine("deg {0}", element.DegreeU); |
| | 251 | | } |
| | 252 | | else |
| | 253 | | { |
| 105 | 254 | | stream.WriteLine("deg {0} {1}", element.DegreeU, element.DegreeV); |
| | 255 | | } |
| | 256 | |
|
| 108 | 257 | | if (element.BasicMatrixU != null) |
| | 258 | | { |
| 105 | 259 | | stream.Write("bmat u"); |
| | 260 | |
|
| 1050 | 261 | | foreach (float value in element.BasicMatrixU) |
| | 262 | | { |
| 420 | 263 | | stream.Write(' '); |
| 420 | 264 | | stream.Write(value.ToString("F6", CultureInfo.InvariantCulture)); |
| | 265 | | } |
| | 266 | |
|
| 105 | 267 | | stream.WriteLine(); |
| | 268 | | } |
| | 269 | |
|
| 108 | 270 | | if (element.BasicMatrixV != null) |
| | 271 | | { |
| 105 | 272 | | stream.Write("bmat v"); |
| | 273 | |
|
| 1050 | 274 | | foreach (float value in element.BasicMatrixV) |
| | 275 | | { |
| 420 | 276 | | stream.Write(' '); |
| 420 | 277 | | stream.Write(value.ToString("F6", CultureInfo.InvariantCulture)); |
| | 278 | | } |
| | 279 | |
|
| 105 | 280 | | stream.WriteLine(); |
| | 281 | | } |
| | 282 | |
|
| 108 | 283 | | if (element.StepV == 1.0f) |
| | 284 | | { |
| 3 | 285 | | stream.WriteLine( |
| 3 | 286 | | "step {0}", |
| 3 | 287 | | element.StepU.ToString("F6", CultureInfo.InvariantCulture)); |
| | 288 | | } |
| | 289 | | else |
| | 290 | | { |
| 105 | 291 | | stream.WriteLine( |
| 105 | 292 | | "step {0} {1}", |
| 105 | 293 | | element.StepU.ToString("F6", CultureInfo.InvariantCulture), |
| 105 | 294 | | element.StepV.ToString("F6", CultureInfo.InvariantCulture)); |
| | 295 | | } |
| | 296 | |
|
| 108 | 297 | | if (element.CurveApproximationTechnique != null) |
| | 298 | | { |
| 105 | 299 | | if (element.CurveApproximationTechnique is ObjConstantParametricSubdivisionTechnique) |
| | 300 | | { |
| 99 | 301 | | var technique = (ObjConstantParametricSubdivisionTechnique)element.CurveApproximationTechnique; |
| 99 | 302 | | stream.WriteLine("ctech cparm {0}", technique.ResolutionU.ToString("F6", CultureInfo.InvariantCultur |
| | 303 | | } |
| 6 | 304 | | else if (element.CurveApproximationTechnique is ObjConstantSpatialSubdivisionTechnique) |
| | 305 | | { |
| 3 | 306 | | var technique = (ObjConstantSpatialSubdivisionTechnique)element.CurveApproximationTechnique; |
| 3 | 307 | | stream.WriteLine("ctech cspace {0}", technique.MaximumLength.ToString("F6", CultureInfo.InvariantCul |
| | 308 | | } |
| 3 | 309 | | else if (element.CurveApproximationTechnique is ObjCurvatureDependentSubdivisionTechnique) |
| | 310 | | { |
| 3 | 311 | | var technique = (ObjCurvatureDependentSubdivisionTechnique)element.CurveApproximationTechnique; |
| 3 | 312 | | stream.WriteLine( |
| 3 | 313 | | "ctech curv {0} {1}", |
| 3 | 314 | | technique.MaximumDistance.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 315 | | technique.MaximumAngle.ToString("F6", CultureInfo.InvariantCulture)); |
| | 316 | | } |
| | 317 | | } |
| | 318 | |
|
| 108 | 319 | | if (element.SurfaceApproximationTechnique != null) |
| | 320 | | { |
| 105 | 321 | | if (element.SurfaceApproximationTechnique is ObjConstantParametricSubdivisionTechnique) |
| | 322 | | { |
| 99 | 323 | | var technique = (ObjConstantParametricSubdivisionTechnique)element.SurfaceApproximationTechnique; |
| | 324 | |
|
| 99 | 325 | | if (technique.ResolutionU == technique.ResolutionV) |
| | 326 | | { |
| 3 | 327 | | stream.WriteLine( |
| 3 | 328 | | "stech cparmb {0}", |
| 3 | 329 | | technique.ResolutionU.ToString("F6", CultureInfo.InvariantCulture)); |
| | 330 | | } |
| | 331 | | else |
| | 332 | | { |
| 96 | 333 | | stream.WriteLine( |
| 96 | 334 | | "stech cparma {0} {1}", |
| 96 | 335 | | technique.ResolutionU.ToString("F6", CultureInfo.InvariantCulture), |
| 96 | 336 | | technique.ResolutionV.ToString("F6", CultureInfo.InvariantCulture)); |
| | 337 | | } |
| | 338 | | } |
| 6 | 339 | | else if (element.SurfaceApproximationTechnique is ObjConstantSpatialSubdivisionTechnique) |
| | 340 | | { |
| 3 | 341 | | var technique = (ObjConstantSpatialSubdivisionTechnique)element.SurfaceApproximationTechnique; |
| 3 | 342 | | stream.WriteLine("stech cspace {0}", technique.MaximumLength.ToString("F6", CultureInfo.InvariantCul |
| | 343 | | } |
| 3 | 344 | | else if (element.SurfaceApproximationTechnique is ObjCurvatureDependentSubdivisionTechnique) |
| | 345 | | { |
| 3 | 346 | | var technique = (ObjCurvatureDependentSubdivisionTechnique)element.SurfaceApproximationTechnique; |
| 3 | 347 | | stream.WriteLine( |
| 3 | 348 | | "stech curv {0} {1}", |
| 3 | 349 | | technique.MaximumDistance.ToString("F6", CultureInfo.InvariantCulture), |
| 3 | 350 | | technique.MaximumAngle.ToString("F6", CultureInfo.InvariantCulture)); |
| | 351 | | } |
| | 352 | | } |
| 6 | 353 | | } |
| | 354 | |
|
| | 355 | | private string GetGroupNames<T>(T element, Func<ObjGroup, List<T>> func) |
| | 356 | | where T : ObjElement |
| | 357 | | { |
| 195 | 358 | | var groups = new List<string>(); |
| | 359 | |
|
| 462 | 360 | | foreach (ObjGroup group in obj.Groups) |
| | 361 | | { |
| 36 | 362 | | List<T> elements = func(group); |
| | 363 | |
|
| 36 | 364 | | if (elements.Contains(element)) |
| | 365 | | { |
| 33 | 366 | | groups.Add(group.Name); |
| | 367 | | } |
| | 368 | | } |
| | 369 | |
|
| 195 | 370 | | return string.Join(" ", groups); |
| | 371 | | } |
| | 372 | | } |
| | 373 | | } |
| | 374 | |
|