Skip to content

Commit c035dec

Browse files
committed
refactor(LineGlyphRepresentation): offset cylinder center
With center offset, don't have to position glyph in center of segment, just position at state origin.
1 parent 3af071b commit c035dec

File tree

1 file changed

+21
-20
lines changed
  • Sources/Widgets/Representations/LineGlyphRepresentation

1 file changed

+21
-20
lines changed

Sources/Widgets/Representations/LineGlyphRepresentation/index.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ function vtkLineGlyphRepresentation(publicAPI, model) {
6767
source: publicAPI,
6868
glyph: vtkCylinderSource.newInstance({
6969
direction: [1, 0, 0],
70+
center: [0.5, 0, 0],
71+
capping: false,
7072
}),
7173
mapper: vtkGlyph3DMapper.newInstance({
7274
orientationArray: 'directions',
@@ -94,48 +96,47 @@ function vtkLineGlyphRepresentation(publicAPI, model) {
9496
allocateSize(internalPolyData, pointCount, model.close && pointCount > 2);
9597

9698
const glyphPositions = internalPolyData.getPoints().getData();
97-
const lines = internalPolyData.getLines().getData();
99+
// There can be only one line.
100+
const segments = internalPolyData.getLines().getData();
98101

99102
const directions = allocateArray(
100103
internalPolyData,
101104
'directions',
102-
lines.length - 1,
105+
segments.length - 1,
103106
undefined,
104107
3
105108
).getData();
106109
const lengths = allocateArray(
107110
internalPolyData,
108111
'lengths',
109-
lines.length - 1,
112+
segments.length - 1,
110113
undefined,
111114
3
112115
).getData();
113116

114-
const pos = []; // scratch
115-
for (let point = 1; point < lines.length - 1; point++) {
117+
const tempVector = []; // scratch
118+
for (let point = 1; point < segments.length - 1; point++) {
119+
const glyph = (point - 1) * 3; // start of glyph's 3 components in the arrays
120+
121+
// With cylinder glyph's offset center, position at state origins.
122+
const origin = points[segments[point]];
123+
[
124+
glyphPositions[glyph],
125+
glyphPositions[glyph + 1],
126+
glyphPositions[glyph + 2],
127+
] = origin;
128+
116129
// Orient glyphs to next point.
117-
const eye = points[lines[point]];
118-
const target = points[lines[point + 1]];
119-
const direction = vtkMath.subtract(target, eye, pos);
120-
const glyph = (point - 1) * 3;
130+
const target = points[segments[point + 1]];
131+
const direction = vtkMath.subtract(target, origin, tempVector);
121132
[directions[glyph], directions[glyph + 1], directions[glyph + 2]] =
122133
direction;
123134

124-
// scale to span between points
135+
// Scale to span between points.
125136
const distance = vec3.length(direction);
126137
lengths[glyph] = distance;
127138
lengths[glyph + 1] = 1;
128139
lengths[glyph + 2] = 1;
129-
130-
// Position glyph at center of line segment.
131-
vec3.normalize(pos, direction);
132-
vec3.scale(pos, pos, distance / 2);
133-
vec3.add(pos, eye, direction);
134-
[
135-
glyphPositions[glyph],
136-
glyphPositions[glyph + 1],
137-
glyphPositions[glyph + 2],
138-
] = pos;
139140
}
140141

141142
internalPolyData.getPoints().modified();

0 commit comments

Comments
 (0)