/
ClipStyle.ts
336 lines (278 loc) · 14.2 KB
/
ClipStyle.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Views
*/
import { assert, JsonUtils } from "@itwin/core-bentley";
import { ViewFlagOverrides } from "./ViewFlags";
import { RgbColor, RgbColorProps } from "./RgbColor";
import { HiddenLine } from "./HiddenLine";
import { FeatureAppearance, FeatureAppearanceProps } from "./FeatureSymbology";
import { ColorDef } from "./ColorDef";
/** Wire format describing a [[CutStyle]] applied to section-cut geometry produced at intersections with a view's [ClipVector]($core-geometry).
* @see [[ClipStyleProps.cutStyle]].
* @public
* @extensions
*/
export interface CutStyleProps {
/** If defined, overrides aspects of the view's [[ViewFlags]] when drawing the cut geometry. */
viewflags?: ViewFlagOverrides;
/** If defined, overrides the view's [[HiddenLine.Settings]] when drawing the cut geometry. */
hiddenLine?: HiddenLine.SettingsProps;
/** If defined, overrides aspects of the cut geometry's symbology. */
appearance?: FeatureAppearanceProps;
}
/** As part of a [[ClipStyle]], describes how section-cut graphics should be displayed.
* @note Section-cut graphics are only produced if [[ClipStyle.produceCutGeometry]] is `true`.
* @public
*/
export class CutStyle {
/** Selectively overrides some of the view's [[ViewFlags]] when drawing the section-cut graphics. */
public readonly viewflags: Readonly<ViewFlagOverrides>;
/** If defined, overrides the settings the view uses to draw the edges of the section-cut graphics. */
public readonly hiddenLine?: HiddenLine.Settings;
/** If defined, overrides aspects of the symbology of the section-cut graphics. */
public readonly appearance?: FeatureAppearance;
/** The default CutStyle, configured to draw the section-cut graphics using the view's settings, with no overrides. */
public static readonly defaults = new CutStyle();
private constructor(viewflags?: Readonly<ViewFlagOverrides>, hiddenLine?: HiddenLine.Settings, appearance?: FeatureAppearance) {
this.viewflags = viewflags ?? {};
if (hiddenLine && !hiddenLine.matchesDefaults)
this.hiddenLine = hiddenLine;
if (appearance && !appearance.matchesDefaults)
this.appearance = appearance;
}
/** Create a CutStyle from its components. */
public static create(viewflags?: Readonly<ViewFlagOverrides>, hiddenLine?: HiddenLine.Settings, appearance?: FeatureAppearance): CutStyle {
if ((viewflags && JsonUtils.isNonEmptyObject(viewflags)) || (hiddenLine && !hiddenLine.matchesDefaults) || (appearance && !appearance.matchesDefaults))
return new CutStyle(viewflags, hiddenLine, appearance);
return this.defaults;
}
public static fromJSON(props?: CutStyleProps): CutStyle {
if (JsonUtils.isNonEmptyObject(props)) {
const viewflags = { ...props?.viewflags };
const hiddenLine = props?.hiddenLine ? HiddenLine.Settings.fromJSON(props.hiddenLine) : undefined;
const appearance = props?.appearance ? FeatureAppearance.fromJSON(props.appearance) : undefined;
return this.create(viewflags, hiddenLine, appearance);
} else {
return this.defaults;
}
}
/** Return JSON representation. The representation is `undefined` if this style matches the default style. */
public toJSON(): CutStyleProps | undefined {
if (this.matchesDefaults)
return undefined;
const props: CutStyleProps = {};
if (JsonUtils.isNonEmptyObject(this.viewflags))
props.viewflags = this.viewflags;
if (this.hiddenLine && !this.hiddenLine.matchesDefaults)
props.hiddenLine = this.hiddenLine?.toJSON();
if (this.appearance && !this.appearance.matchesDefaults)
props.appearance = this.appearance.toJSON();
return props;
}
/** Returns true if this style matches the default style - that is, it overrides none of the view's settings. */
public get matchesDefaults(): boolean {
if (this === CutStyle.defaults)
return true;
return !JsonUtils.isNonEmptyObject(this.viewflags) && (!this.hiddenLine || this.hiddenLine.matchesDefaults) && (!this.appearance || this.appearance.matchesDefaults);
}
}
/** Wire format describing a [[ClipIntersectionStyle]].
* @see [[ClipStyleProps.ClipIntersectionStyle]].
* @public
* @extensions
*/
export interface ClipIntersectionStyleProps {
/** Color to apply to intersection of geometry and clip planes, default white */
color?: RgbColorProps;
/** Number of pixels to be considered intersecting the clip plane, default 1 */
width?: number;
}
/** As part of a [[ClipStyle]], describes how to colorize geometry intersecting the clip planes.
* @note Edges are highlighted only if [[ClipStyle.ClipIntersectionStyle]] is `true`.
* @public
* @extensions
*/
export class ClipIntersectionStyle {
/** Color to apply to intersection of geometry and clip planes, default white */
public readonly color: RgbColor;
/** Number of pixels to be considered intersecting the clip plane, default 1 */
public readonly width: number;
private constructor(color: RgbColor = RgbColor.fromColorDef(ColorDef.white), width: number = 1) {
this.color = color;
this.width = width;
}
/** Create a highlight from its components. */
public static create(color?: RgbColor, width?: number): ClipIntersectionStyle {
if (!color && !width)
return this.defaults;
return new ClipIntersectionStyle(color, width);
}
public static readonly defaults = new ClipIntersectionStyle();
public static fromJSON(props?: ClipIntersectionStyleProps): ClipIntersectionStyle {
if (props === undefined) {
return ClipIntersectionStyle.defaults;
}
const color = props.color ? RgbColor.fromJSON(props.color) : RgbColor.fromColorDef(ColorDef.white);
const width = props.width ? props.width : 1;
return new ClipIntersectionStyle(color, width);
}
/** The JSON representation of this style. It is `undefined` if this style matches the defaults. */
public toJSON(): ClipIntersectionStyleProps | undefined {
const props: ClipIntersectionStyleProps = {};
if (this.matchesDefaults) {
return undefined;
}
if (this.color)
props.color = this.color.toJSON();
if (this.width)
props.width = this.width;
return props;
}
public get matchesDefaults(): boolean {
if (this === ClipIntersectionStyle.defaults)
return true;
return !this.color && !this.width;
}
}
/** Arguments supplied to [[ClipStyle.create]].
* @public
* @extensions
*/
export interface ClipStyleCreateArgs {
/** If `true`, geometry will be produced at the clip planes in a 3d view.
* - Solids (closed volumes) will produce facets on the clip planes.
* - Other surfaces will produce line strings representing the edges of the surface at the clip planes.
* @note Cut geometry will only be produced for element geometry - not for, e.g., terrain or reality models.
*/
produceCutGeometry?: boolean;
/** If `true`, intersection of geometry and clip planes will be colorized */
colorizeIntersection?: boolean;
/** Controls aspects of how the cut geometry is displayed, if [[produceCutGeometry]] is `true`. */
cutStyle?: CutStyle;
/** If defined, geometry inside the clip planes will be drawn in this color. */
insideColor?: RgbColor;
/** If defined, geometry outside of the clip planes will be drawn in this color instead of being clipped. */
outsideColor?: RgbColor;
/** Controls the style of the intersection of geometry and clip planes */
intersectionStyle?: ClipIntersectionStyle;
}
/** Wire format describing a [[ClipStyle]].
* @see [[DisplayStyleSettingsProps.clipStyle]].
* @public
* @extensions
*/
export interface ClipStyleProps {
/** If `true`, geometry will be produced at the clip planes in a 3d view.
* - Solids (closed volumes) will produce facets on the clip planes.
* - Other surfaces will produce line strings representing the edges of the surface at the clip planes.
* @note Cut geometry will only be produced for element geometry - not for, e.g., terrain or reality models.
*/
produceCutGeometry?: boolean;
/** If 'true', intersection of geometry and clip planes will be colorized */
colorizeIntersection?: boolean;
/** Controls aspects of how the cut geometry is displayed, if [[produceCutGeometry]] is `true`. */
cutStyle?: CutStyleProps;
/** If defined, geometry inside the clip planes will be drawn in this color. */
insideColor?: RgbColorProps;
/** If defined, geometry outside of the clip planes will be drawn in this color instead of being clipped. */
outsideColor?: RgbColorProps;
/** Controls the style of the intersection of geometry and clip planes */
intersectionStyle?: ClipIntersectionStyleProps;
}
/** Describes symbology and behavior applied to a [ClipVector]($core-geometry) when applied to a [ViewState]($frontend) or [[ModelClipGroup]].
* @see [[DisplayStyleSettings.clipStyle]].
* @public
*/
export class ClipStyle {
/** If `true`, geometry will be produced at the clip planes.
* - Solids (closed volumes) will produce facets on the clip planes.
* - Other surfaces will produce line strings representing the edges of the surface at the clip planes.
* @note Cut geometry will only be produced for element geometry - not for, e.g., terrain or reality models.
*/
public readonly produceCutGeometry: boolean;
/** If 'true', intersection of geometry and clip planes will be colorized */
public readonly colorizeIntersection: boolean;
/** Controls aspects of how the cut geometry is displayed, if [[produceCutGeometry]] is `true`. */
public readonly cutStyle: CutStyle;
/** If defined, geometry inside the clip planes will be drawn in this color. */
public readonly insideColor?: RgbColor;
/** If defined, geometry outside of the clip planes will be drawn in this color instead of being clipped. */
public readonly outsideColor?: RgbColor;
/** Controls the style of the intersection of geometry and clip planes */
public readonly intersectionStyle?: ClipIntersectionStyle;
/** The default style, which overrides none of the view's settings. */
public static readonly defaults = new ClipStyle(false, false, CutStyle.defaults, undefined, undefined, undefined);
private constructor(produceCutGeometry: boolean, colorizeIntersection: boolean, cutStyle: CutStyle, inside: RgbColor | undefined, outside: RgbColor | undefined, intersectionStyle: ClipIntersectionStyle | undefined) {
this.produceCutGeometry = produceCutGeometry;
this.colorizeIntersection = colorizeIntersection;
this.cutStyle = cutStyle;
this.insideColor = inside;
this.outsideColor = outside;
this.intersectionStyle = intersectionStyle;
}
/** @deprecated in 4.x. Use [[create(style: ClipStyleCreateArgs]] */
public static create(produceCutGeometry: boolean, cutStyle: CutStyle, insideColor?: RgbColor, outsideColor?: RgbColor): ClipStyle;
/** Create a style from its components. */
public static create(style: ClipStyleCreateArgs): ClipStyle;
/** @internal */
public static create(styleOrProduceCutGeometry: ClipStyleCreateArgs | boolean, cutStyle?: CutStyle, insideColor?: RgbColor, outsideColor?: RgbColor): ClipStyle {
if (typeof styleOrProduceCutGeometry === "boolean") {
cutStyle = cutStyle === undefined ? CutStyle.defaults : cutStyle;
if (!styleOrProduceCutGeometry && cutStyle.matchesDefaults && !insideColor && !outsideColor) {
return this.defaults;
}
return new ClipStyle(styleOrProduceCutGeometry, false, cutStyle, insideColor, outsideColor, undefined);
}
const style = styleOrProduceCutGeometry;
if (!style.produceCutGeometry && !style.colorizeIntersection && (!style.cutStyle || style.cutStyle.matchesDefaults) && !style.insideColor && !style.outsideColor && !style.intersectionStyle)
return this.defaults;
const produceCutGeometry = style.produceCutGeometry ? true : false;
const colorizeIntersection = style.colorizeIntersection ? true : false;
cutStyle = style.cutStyle === undefined ? CutStyle.defaults : style.cutStyle;
return new ClipStyle(produceCutGeometry, colorizeIntersection, cutStyle, style.insideColor, style.outsideColor, style.intersectionStyle);
}
public static fromJSON(props?: ClipStyleProps): ClipStyle {
if (JsonUtils.isNonEmptyObject(props)) {
const produceCutGeometry = props.produceCutGeometry ?? false;
const colorizeIntersection = props.colorizeIntersection ? true : false;
const cutStyle = CutStyle.fromJSON(props.cutStyle);
const insideColor = props.insideColor ? RgbColor.fromJSON(props.insideColor) : undefined;
const outsideColor = props.outsideColor ? RgbColor.fromJSON(props.outsideColor) : undefined;
const intersectionStyle = props.intersectionStyle ? ClipIntersectionStyle.fromJSON(props.intersectionStyle) : undefined;
return this.create({produceCutGeometry, colorizeIntersection, cutStyle, insideColor, outsideColor, intersectionStyle});
}
return this.defaults;
}
/** The JSON representation of this style. It is `undefined` if this style matches the defaults. */
public toJSON(): ClipStyleProps | undefined {
if (this.matchesDefaults)
return undefined;
const props: ClipStyleProps = {};
if (this.produceCutGeometry)
props.produceCutGeometry = true;
if (this.colorizeIntersection)
props.colorizeIntersection = true;
const cutStyle = this.cutStyle.toJSON();
if (cutStyle) {
assert(!this.cutStyle.matchesDefaults);
props.cutStyle = cutStyle;
}
if (this.insideColor)
props.insideColor = this.insideColor.toJSON();
if (this.outsideColor)
props.outsideColor = this.outsideColor.toJSON();
if (this.intersectionStyle)
props.intersectionStyle = this.intersectionStyle.toJSON();
return props;
}
/** Returns true if this style matches the [[ClipStyle.defaults]] - that is, it overrides no settings from the view. */
public get matchesDefaults(): boolean {
if (this === ClipStyle.defaults)
return true;
return !this.produceCutGeometry && !this.colorizeIntersection && !this.insideColor && !this.outsideColor && this.cutStyle.matchesDefaults && !this.intersectionStyle;
}
}