| | | 1 | | // <copyright file="ObjFileReaderContext.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 | | namespace JeremyAnsel.Media.WavefrontObj; |
| | | 9 | | |
| | | 10 | | internal class ObjFileReaderContext |
| | | 11 | | { |
| | | 12 | | private readonly ObjFile _obj; |
| | | 13 | | |
| | 4 | 14 | | public ObjFileReaderContext(ObjFile obj, ObjFileReaderSettings settings) |
| | | 15 | | { |
| | 4 | 16 | | _obj = obj; |
| | 4 | 17 | | Settings = settings; |
| | | 18 | | |
| | 4 | 19 | | GroupNames = new List<string>(); |
| | 4 | 20 | | } |
| | | 21 | | |
| | 4 | 22 | | public ObjFileReaderSettings Settings { get; } |
| | | 23 | | |
| | 4 | 24 | | public string? ObjectName { get; set; } |
| | | 25 | | |
| | 4 | 26 | | public int LevelOfDetail { get; set; } |
| | | 27 | | |
| | 4 | 28 | | public string? MapName { get; set; } |
| | | 29 | | |
| | 4 | 30 | | public string? MaterialName { get; set; } |
| | | 31 | | |
| | 4 | 32 | | public long SmoothingGroupNumber { get; set; } |
| | | 33 | | |
| | 4 | 34 | | public bool IsBevelInterpolationEnabled { get; set; } |
| | | 35 | | |
| | 4 | 36 | | public bool IsColorInterpolationEnabled { get; set; } |
| | | 37 | | |
| | 4 | 38 | | public bool IsDissolveInterpolationEnabled { get; set; } |
| | | 39 | | |
| | 4 | 40 | | public int MergingGroupNumber { get; set; } |
| | | 41 | | |
| | 4 | 42 | | public ObjFreeFormType FreeFormType { get; set; } |
| | | 43 | | |
| | 4 | 44 | | public bool IsRationalForm { get; set; } |
| | | 45 | | |
| | 4 | 46 | | public int DegreeU { get; set; } |
| | | 47 | | |
| | 4 | 48 | | public int DegreeV { get; set; } |
| | | 49 | | |
| | 4 | 50 | | public float[]? BasicMatrixU { get; set; } |
| | | 51 | | |
| | 4 | 52 | | public float[]? BasicMatrixV { get; set; } |
| | | 53 | | |
| | 4 | 54 | | public float StepU { get; set; } |
| | | 55 | | |
| | 4 | 56 | | public float StepV { get; set; } |
| | | 57 | | |
| | 4 | 58 | | public ObjApproximationTechnique? CurveApproximationTechnique { get; set; } |
| | | 59 | | |
| | 4 | 60 | | public ObjApproximationTechnique? SurfaceApproximationTechnique { get; set; } |
| | | 61 | | |
| | 4 | 62 | | public ObjFreeFormElement? CurrentFreeFormElement { get; set; } |
| | | 63 | | |
| | 4 | 64 | | public List<string> GroupNames { get; private set; } |
| | | 65 | | |
| | | 66 | | private List<ObjGroup>? _currentGroups; |
| | | 67 | | |
| | | 68 | | public List<ObjGroup> GetCurrentGroups() |
| | | 69 | | { |
| | 4 | 70 | | if (_currentGroups is not null) return _currentGroups; |
| | 4 | 71 | | _currentGroups = [_obj.DefaultGroup]; |
| | 4 | 72 | | return _currentGroups; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public void CreateGroups() |
| | | 76 | | { |
| | 4 | 77 | | _currentGroups = new List<ObjGroup>(); |
| | 4 | 78 | | foreach (var name in this.GroupNames) |
| | | 79 | | { |
| | 4 | 80 | | ObjGroup? group = null; |
| | 4 | 81 | | if (!Settings.HandleEachGroupOccurrenceAsNewGroup) |
| | 4 | 82 | | group = GetGroupByName(name); |
| | | 83 | | |
| | 4 | 84 | | if (group is null) |
| | | 85 | | { |
| | 4 | 86 | | group = new ObjGroup(name); |
| | 4 | 87 | | _obj.Groups.Add(group); |
| | | 88 | | } |
| | | 89 | | |
| | 4 | 90 | | _currentGroups.Add(group); |
| | | 91 | | } |
| | | 92 | | |
| | 4 | 93 | | if (_currentGroups.Count == 0) |
| | | 94 | | { |
| | 4 | 95 | | _currentGroups.Add(_obj.DefaultGroup); |
| | | 96 | | } |
| | 4 | 97 | | } |
| | | 98 | | |
| | | 99 | | private ObjGroup? GetGroupByName(string name) |
| | 4 | 100 | | => _obj.Groups.Find(t => string.Equals(t.Name, name, StringComparison.Ordinal)); |
| | | 101 | | |
| | | 102 | | public void ApplyAttributesToElement(ObjElement element) |
| | | 103 | | { |
| | 4 | 104 | | element.ObjectName = this.ObjectName; |
| | 4 | 105 | | element.LevelOfDetail = this.LevelOfDetail; |
| | 4 | 106 | | element.MapName = this.MapName; |
| | 4 | 107 | | element.MaterialName = this.MaterialName; |
| | 4 | 108 | | } |
| | | 109 | | |
| | | 110 | | public void ApplyAttributesToPolygonalElement(ObjPolygonalElement element) |
| | | 111 | | { |
| | 4 | 112 | | element.SmoothingGroupNumber = this.SmoothingGroupNumber; |
| | 4 | 113 | | element.IsBevelInterpolationEnabled = this.IsBevelInterpolationEnabled; |
| | 4 | 114 | | element.IsColorInterpolationEnabled = this.IsColorInterpolationEnabled; |
| | 4 | 115 | | element.IsDissolveInterpolationEnabled = this.IsDissolveInterpolationEnabled; |
| | 4 | 116 | | } |
| | | 117 | | |
| | | 118 | | public void ApplyAttributesToFreeFormElement(ObjFreeFormElement element) |
| | | 119 | | { |
| | 4 | 120 | | element.MergingGroupNumber = this.MergingGroupNumber; |
| | 4 | 121 | | element.FreeFormType = this.FreeFormType; |
| | 4 | 122 | | element.IsRationalForm = this.IsRationalForm; |
| | 4 | 123 | | element.DegreeU = this.DegreeU; |
| | 4 | 124 | | element.DegreeV = this.DegreeV; |
| | 4 | 125 | | element.BasicMatrixU = this.BasicMatrixU; |
| | 4 | 126 | | element.BasicMatrixV = this.BasicMatrixV; |
| | 4 | 127 | | element.StepU = this.StepU; |
| | 4 | 128 | | element.StepV = this.StepV; |
| | 4 | 129 | | element.CurveApproximationTechnique = this.CurveApproximationTechnique; |
| | 4 | 130 | | element.SurfaceApproximationTechnique = this.SurfaceApproximationTechnique; |
| | 4 | 131 | | } |
| | | 132 | | } |