/
TaggedNumericData.ts
166 lines (160 loc) · 5.8 KB
/
TaggedNumericData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Polyface
*/
import { Geometry } from "../Geometry";
/**
* `TaggedNumericConstants` defines enums with constant values for use in tags of [[TaggedNumericData]]
* @public
*/
export namespace TaggedNumericConstants {
/** Reserved values for the "tagA" member of [[TaggedNumericData]]
* @public
*
*/
export enum TaggedNumericTagType {
/** `tagA` value identifying a subdivision surface*/
SubdivisionSurface = -1000
}
/**
* `tagB` values for supported types of subdivision surfaces
* @public
*/
export enum SubdivisionMethod {
ChooseBasedOnFacets = 0,
CatmullClark = 1,
Loop = 2,
DooSabin = 3
}
/**
* numeric values for subdivision control. These are entered in the intData array as first of a pair.
* @public
*/
export enum SubdivisionControlCode {
/** pair (FixedDepth, d) indicates subdivision to depth d */
FixedDepth = -100,
/** pair (FixedDepth, index) indicates absolute tolerance with value in doubleData[index] */
AbsoluteTolerance = -101,
/** pair (FixedDepth, index) indicates tolerance as a fraction of base mesh range is found in doubleData[index] */
FractionOfRangeBoxTolerance = -102
}
}
/**
* Structure with 2 integer tags and optional arrays of integers, doubles, points, vectors, and geometry.
* * In typescript/javascript, all integer numbers that can be non-integer. Please do not insert non-integers in the integer array.
* @public
*/
export class TaggedNumericData {
/** Application specific primary tag. See reserved values in [[TaggedNumericConstants]] */
public tagA: number;
/** Application specific secondary tag. See reserved values in [[TaggedNumericConstants]] */
public tagB: number;
public constructor(tagA: number = 0, tagB: number = 0,
intData?: number[], doubleData?: number[],
) {
this.tagA = tagA;
this.tagB = tagB;
if (intData) this.intData = intData;
if (doubleData) this.doubleData = doubleData;
}
/** Integer data with application-specific meaning */
public intData?: number[];
/** Double data with application-specific meaning */
public doubleData?: number[];
/**
* push a pair of int values on the intData array.
* @param intA
* @param intB
*/
public pushIntPair(intA: number, intB: number) {
if (!this.intData) this.intData = [];
this.intData.push(intA);
this.intData.push(intB);
}
/**
* push a pair of int values on the intData array.
* @param intA int to push on the intData array, followed by index of valueB in the doubleData array.
* @param valueB value to push on the doubleData array.
*/
public pushIndexedDouble(intA: number, valueB: number) {
if (!this.intData) this.intData = [];
if (!this.doubleData) this.doubleData = [];
this.intData.push(intA);
this.intData.push(this.doubleData.length);
this.doubleData.push(valueB);
}
/**
* Search pairs in the intData array for a pair (targetTag, value). Return the value, possibly restricted to (minValue,maxValue)
* @param targetTag
* @param minValue
* @param maxValue
* @param defaultValue
*/
public tagToInt(targetTag: number, minValue: number, maxValue: number, defaultValue: number): number {
if (this.intData) {
for (let i = 0; i + 1 < this.intData.length; i += 2){
if (this.intData[i] === targetTag)
return Math.min(Math.max(this.intData[i + 1], minValue), maxValue);
}
}
return defaultValue;
}
/**
* Search pairs in the intData array for a pair (targetTag, index). Return getDoubleData[index] value, possibly restricted to (minValue,maxValue)
* @param targetTag
* @param minValue
* @param maxValue
* @param defaultValue
*/
public tagToIndexedDouble(targetTag: number, minValue: number, maxValue: number, defaultValue: number): number {
if (this.intData) {
for (let i = 0; i + 1 < this.intData.length; i += 2){
if (this.intData[i] === targetTag) {
return Geometry.clamp(this.getDoubleData (this.intData[i + 1], defaultValue), minValue, maxValue);
}
}
}
return defaultValue;
}
/**
* get doubleData[index], or indicated default if the index is out of range
* @param index
* @param defaultValue
*/
public getDoubleData(index: number, defaultValue: number): number{
if (this.doubleData && 0 <= index && index < this.doubleData.length)
return this.doubleData[index];
return defaultValue;
}
/** Apply isAlmostEqual to all members. */
public isAlmostEqual(other: TaggedNumericData): boolean{
if (other === undefined)
return false;
if (this.tagA !== other.tagA)
return false;
if (this.tagB !== other.tagB)
return false;
return Geometry.exactEqualNumberArrays(this.intData, other.intData)
&& Geometry.almostEqualArrays<number>(this.doubleData, other.doubleData, (a, b) => Geometry.isAlmostEqualNumber(a, b));
}
public static areAlmostEqual(dataA: TaggedNumericData | undefined, dataB: TaggedNumericData | undefined): boolean{
if (dataA === undefined && dataB === undefined)
return true;
if (dataA !== undefined && dataB !== undefined)
return dataA.isAlmostEqual(dataB);
return false;
}
/** Return a deep clone. */
public clone(result?: TaggedNumericData): TaggedNumericData {
if (!result)
result = new TaggedNumericData(this.tagA, this.tagB);
if (this.intData)
result.intData = this.intData.slice();
if (this.doubleData)
result.doubleData = this.doubleData.slice();
return result;
}
}