|
@@ -1,5 +1,5 @@
|
|
|
/**
|
|
|
- * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
|
|
|
+ * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
|
|
|
*
|
|
|
* @author Alexander Rose <alexander.rose@weirdbyte.de>
|
|
|
* @author David Sehnal <david.sehnal@gmail.com>
|
|
@@ -9,11 +9,11 @@ import { Vec3 } from '../../../../mol-math/linear-algebra';
|
|
|
import { ChunkedArray } from '../../../../mol-data/util';
|
|
|
import { MeshBuilder } from '../mesh-builder';
|
|
|
|
|
|
-const normalVector = Vec3.zero();
|
|
|
-const surfacePoint = Vec3.zero();
|
|
|
-const controlPoint = Vec3.zero();
|
|
|
-const u = Vec3.zero();
|
|
|
-const v = Vec3.zero();
|
|
|
+const normalVector = Vec3();
|
|
|
+const surfacePoint = Vec3();
|
|
|
+const controlPoint = Vec3();
|
|
|
+const u = Vec3();
|
|
|
+const v = Vec3();
|
|
|
|
|
|
function add2AndScale2(out: Vec3, a: Vec3, b: Vec3, sa: number, sb: number) {
|
|
|
out[0] = (a[0] * sa) + (b[0] * sb);
|
|
@@ -27,11 +27,28 @@ function add3AndScale2(out: Vec3, a: Vec3, b: Vec3, c: Vec3, sa: number, sb: num
|
|
|
out[2] = (a[2] * sa) + (b[2] * sb) + c[2];
|
|
|
}
|
|
|
|
|
|
-export function addTube(state: MeshBuilder.State, controlPoints: ArrayLike<number>, normalVectors: ArrayLike<number>, binormalVectors: ArrayLike<number>, linearSegments: number, radialSegments: number, widthValues: ArrayLike<number>, heightValues: ArrayLike<number>, waveFactor: number, startCap: boolean, endCap: boolean) {
|
|
|
+
|
|
|
+const CosSinCache = new Map<number, { cos: number[], sin: number[] }>();
|
|
|
+function getCosSin(radialSegments: number) {
|
|
|
+ if (!CosSinCache.has(radialSegments)) {
|
|
|
+ const cos: number[] = [];
|
|
|
+ const sin: number[] = [];
|
|
|
+ for (let j = 0; j < radialSegments; ++j) {
|
|
|
+ const t = 2 * Math.PI * j / radialSegments;
|
|
|
+ cos[j] = Math.cos(t);
|
|
|
+ sin[j] = Math.sin(t);
|
|
|
+ }
|
|
|
+ CosSinCache.set(radialSegments, { cos, sin });
|
|
|
+ }
|
|
|
+ return CosSinCache.get(radialSegments)!;
|
|
|
+}
|
|
|
+
|
|
|
+export function addTube(state: MeshBuilder.State, controlPoints: ArrayLike<number>, normalVectors: ArrayLike<number>, binormalVectors: ArrayLike<number>, linearSegments: number, radialSegments: number, widthValues: ArrayLike<number>, heightValues: ArrayLike<number>, startCap: boolean, endCap: boolean) {
|
|
|
const { currentGroup, vertices, normals, indices, groups } = state;
|
|
|
|
|
|
let vertexCount = vertices.elementCount;
|
|
|
- const di = 1 / linearSegments;
|
|
|
+
|
|
|
+ const { cos, sin } = getCosSin(radialSegments);
|
|
|
|
|
|
for (let i = 0; i <= linearSegments; ++i) {
|
|
|
const i3 = i * 3;
|
|
@@ -42,21 +59,12 @@ export function addTube(state: MeshBuilder.State, controlPoints: ArrayLike<numbe
|
|
|
const width = widthValues[i];
|
|
|
const height = heightValues[i];
|
|
|
|
|
|
- const tt = di * i - 0.5;
|
|
|
- const ff = 1 + (waveFactor - 1) * (Math.cos(2 * Math.PI * tt) + 1);
|
|
|
- const w = ff * width, h = ff * height;
|
|
|
-
|
|
|
for (let j = 0; j < radialSegments; ++j) {
|
|
|
- const t = 2 * Math.PI * j / radialSegments;
|
|
|
-
|
|
|
- add3AndScale2(surfacePoint, u, v, controlPoint, h * Math.cos(t), w * Math.sin(t));
|
|
|
+ add3AndScale2(surfacePoint, u, v, controlPoint, height * cos[j], width * sin[j]);
|
|
|
if (radialSegments === 2) {
|
|
|
- // add2AndScale2(normalVector, u, v, w * Math.cos(t), h * Math.sin(t))
|
|
|
- Vec3.copy(normalVector, v);
|
|
|
- Vec3.normalize(normalVector, normalVector);
|
|
|
- if (t !== 0 || i % 2 === 0) Vec3.negate(normalVector, normalVector);
|
|
|
+ if (j !== 0 || i % 2 === 0) v3negate(normalVector, normalVector);
|
|
|
} else {
|
|
|
- add2AndScale2(normalVector, u, v, w * Math.cos(t), h * Math.sin(t));
|
|
|
+ add2AndScale2(normalVector, u, v, width * cos[j], height * sin[j]);
|
|
|
}
|
|
|
Vec3.normalize(normalVector, normalVector);
|
|
|
|
|
@@ -98,9 +106,7 @@ export function addTube(state: MeshBuilder.State, controlPoints: ArrayLike<numbe
|
|
|
|
|
|
vertexCount = vertices.elementCount;
|
|
|
for (let i = 0; i < radialSegments; ++i) {
|
|
|
- const t = 2 * Math.PI * i / radialSegments;
|
|
|
-
|
|
|
- add3AndScale2(surfacePoint, u, v, controlPoint, height * Math.cos(t), width * Math.sin(t));
|
|
|
+ add3AndScale2(surfacePoint, u, v, controlPoint, height * cos[i], width * sin[i]);
|
|
|
|
|
|
ChunkedArray.add3(vertices, surfacePoint[0], surfacePoint[1], surfacePoint[2]);
|
|
|
ChunkedArray.add3(normals, normalVector[0], normalVector[1], normalVector[2]);
|
|
@@ -130,9 +136,7 @@ export function addTube(state: MeshBuilder.State, controlPoints: ArrayLike<numbe
|
|
|
|
|
|
vertexCount = vertices.elementCount;
|
|
|
for (let i = 0; i < radialSegments; ++i) {
|
|
|
- const t = 2 * Math.PI * i / radialSegments;
|
|
|
-
|
|
|
- add3AndScale2(surfacePoint, u, v, controlPoint, height * Math.cos(t), width * Math.sin(t));
|
|
|
+ add3AndScale2(surfacePoint, u, v, controlPoint, height * cos[i], width * sin[i]);
|
|
|
|
|
|
ChunkedArray.add3(vertices, surfacePoint[0], surfacePoint[1], surfacePoint[2]);
|
|
|
ChunkedArray.add3(normals, normalVector[0], normalVector[1], normalVector[2]);
|