-
Notifications
You must be signed in to change notification settings - Fork 208
/
FacetOrientation.ts
206 lines (202 loc) · 8.2 KB
/
FacetOrientation.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*---------------------------------------------------------------------------------------------
* 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 { IndexedEdgeMatcher, SortableEdge } from "./IndexedEdgeMatcher";
import { IndexedPolyface, PolyfaceVisitor } from "./Polyface";
import { PolyfaceQuery } from "./PolyfaceQuery";
class OrientedComponentData {
public numPositive: number;
public numNegative: number;
public firstEdgeIndex: number;
public constructor(firstEdgeIndex: number) {
this.numPositive = this.numNegative = 0;
this.firstEdgeIndex = firstEdgeIndex;
}
// announce a new facet orientation.
public recordOrientation(orientation: number) {
if (orientation > 0)
this.numPositive++;
else if (orientation < 0)
this.numNegative++;
}
}
export class FacetOrientationFixup {
private _edges: IndexedEdgeMatcher;
private _edgeToPartnerEdge: number[];
private _edgeToEdgeInComponent: number[];
private _facetToFirstEdgeInComponent: number[];
private _facetOrientation: number[];
private _components: OrientedComponentData[];
private _visitor: PolyfaceVisitor;
private _mesh: IndexedPolyface;
private constructor(mesh: IndexedPolyface) {
this._visitor = mesh.createVisitor(1);
this._edges = PolyfaceQuery.createIndexedEdges(this._visitor);
this._edgeToPartnerEdge = [];
this._edgeToEdgeInComponent = [];
this._facetToFirstEdgeInComponent = [];
this._facetOrientation = [];
this._components = [];
this._mesh = mesh;
}
private edgeIdToFacetOrientation(edgeId: number): number {
const facetIndex = this._edges.edges[edgeId].facetIndex;
return this._facetOrientation[facetIndex];
}
/**
* RETURN FALSE IF ANY EDGE HAS 3 ORE MORE FACETS
*/
private setupUnoriented(): boolean {
this._edges.sort();
const edgeArray = this._edges.edges;
// make each edge a singleton with itself as successor . .
let maxFacetIndex = -1;
const numEdges = this._edges.edges.length;
for (let i = 0; i < numEdges; i++) {
const facetIndex = this._edges.edges[i].facetIndex;
if (facetIndex > maxFacetIndex)
maxFacetIndex = facetIndex;
this._edgeToEdgeInComponent.push(i);
this._edgeToPartnerEdge.push(i);
}
for (let i = 0; i <= maxFacetIndex; i++) {
this._facetToFirstEdgeInComponent.push(-1);
this._facetOrientation.push(0);
}
// make each edge part of a component for only its own facet
// (This component exists only ephemerally -- it is not referenced by true component)
// (swapping entries in the cyclic loops merges loops.)
for (let i = 0; i < numEdges; i++) {
const facetIndex = this._edges.edges[i].facetIndex;
const j = this._facetToFirstEdgeInComponent[facetIndex];
if (j === -1)
this._facetToFirstEdgeInComponent[facetIndex] = i;
else {
FacetOrientationFixup.swapEntries(this._edgeToEdgeInComponent, i, j);
}
}
// make contiguous partner edges cycles in edgeToPartnerEdge.
// edge i is initially always a singleton.
for (let edgeIndex0 = 0; edgeIndex0 < numEdges; edgeIndex0++) {
let edgeIndex1 = edgeIndex0 + 1;
while (edgeIndex1 < numEdges && SortableEdge.areUndirectedPartners(edgeArray[edgeIndex0], edgeArray[edgeIndex1])) {
// splice the loops ...
FacetOrientationFixup.swapEntries(this._edgeToPartnerEdge, edgeIndex0, edgeIndex1);
edgeIndex1++;
}
// BUT .. everything else will fail if more than 2 anywhere .....
if (edgeIndex1 > edgeIndex0 + 2)
return false;
}
return true;
}
private recordFacetInComponent(facetIndex: number, orientation: number) {
const componentData = this._components[this._components.length - 1];
this._facetOrientation[facetIndex] = orientation;
componentData.recordOrientation(orientation);
}
private initializeComponent(edgeIndex: number) {
const facetIndex = this._edges.edges[edgeIndex].facetIndex;
this._components.push(new OrientedComponentData(edgeIndex));
this.recordFacetInComponent(facetIndex, 1);
}
private _workArray: number[] = [];
private pushFacetEdgesOnStack(seedEdge: number, stack: number[]) {
FacetOrientationFixup.extractCyclicIndices(this._edgeToEdgeInComponent, seedEdge, this._workArray);
for (const edgeIndex of this._workArray) {
stack.push(edgeIndex);
}
}
/**
* * Run flood search from every possible seed, assigning positive and negative orientations
* * Halt and return false if flood detects Klein bottle effects.
* @return true if flood
*/
private doFlood(): boolean {
// Finally time for flood search.
const edgeStack: number[] = [];
const edgeArray = this._edges.edges;
const numEdges = edgeArray.length;
const neighborEdges: number[] = [];
for (let seedEdgeIndex = 0; seedEdgeIndex < numEdges; seedEdgeIndex++) {
if (this.edgeIdToFacetOrientation(seedEdgeIndex) === 0) {
edgeStack.length = 0;
this.initializeComponent(seedEdgeIndex);
this.pushFacetEdgesOnStack(seedEdgeIndex, edgeStack);
// An edge on the stack is part of the current component.
// Its partners may or may not have been visited.
let baseEdgeIndex: number | undefined;
while (undefined !== (baseEdgeIndex = edgeStack.pop())) {
const baseFacet = edgeArray[baseEdgeIndex].facetIndex;
const baseOrientation = this._facetOrientation[baseFacet];
FacetOrientationFixup.extractCyclicIndices(this._edgeToPartnerEdge, baseEdgeIndex, neighborEdges);
for (const neighborEdgeIndex of neighborEdges) {
if (neighborEdgeIndex !== baseEdgeIndex) {
const neighborFacet = edgeArray[neighborEdgeIndex].facetIndex;
const neighborOrientation = this._facetOrientation[neighborFacet];
if (neighborOrientation === 0) {
// first visit to this facet !
// orientations of baseEdge and neighborEdge tell us how to orient it.
const newOrientation = SortableEdge.areDirectedPartners(edgeArray[baseEdgeIndex], edgeArray[neighborEdgeIndex]) ? baseOrientation : -baseOrientation;
this.recordFacetInComponent(neighborFacet, newOrientation);
this.pushFacetEdgesOnStack(neighborEdgeIndex, edgeStack);
FacetOrientationFixup.swapEntries(this._edgeToEdgeInComponent, baseEdgeIndex, neighborEdgeIndex);
} else {
// looking across to an already-visited facet ..
const edgeOrientation = SortableEdge.relativeOrientation(edgeArray[baseEdgeIndex], edgeArray[neighborEdgeIndex]);
if (edgeOrientation * baseOrientation * neighborOrientation > 0)
return false;
}
}
}
}
}
}
// fall through when floods have completed in all components, with no Klein bottle effects
return true;
}
private doFacetReversals(): number {
let numReverse = 0;
for (this._visitor.reset(); this._visitor.moveToNextFacet();) {
const facetIndex = this._visitor.currentReadIndex();
if (this._facetOrientation[facetIndex] < 0) {
numReverse++;
this._mesh.reverseSingleFacet(facetIndex);
}
}
return numReverse;
}
public static doFixup(mesh: IndexedPolyface): boolean {
const context = new FacetOrientationFixup(mesh);
if (!context.setupUnoriented())
return false;
const ok = context.doFlood();
if (ok)
context.doFacetReversals();
return ok;
}
/** swap entries at indices in a number array.
* * indices are not checked for validity.
*/
private static swapEntries(data: number[], i: number, j: number) {
const q = data[i];
data[i] = data[j];
data[j] = q;
}
/**
*
* @param data an array of cyclically linked loops.
*/
private static extractCyclicIndices(data: number[], index0: number, loopIndices: number[]) {
loopIndices.length = 0;
let i = index0;
do {
loopIndices.push(i);
i = data[i];
} while (i !== index0);
}
}