本文整理汇总了TypeScript中math/polyhedra.Polyhedron.withChanges方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Polyhedron.withChanges方法的具体用法?TypeScript Polyhedron.withChanges怎么用?TypeScript Polyhedron.withChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/polyhedra.Polyhedron
的用法示例。
在下文中一共展示了Polyhedron.withChanges方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: removeCap
function removeCap(polyhedron: Polyhedron, cap: Cap) {
return removeExtraneousVertices(
polyhedron.withChanges(solid =>
solid.withoutFaces(cap.faces()).addFaces([cap.boundary().vertices]),
),
);
}
示例2: duplicateVertices
function duplicateVertices(
polyhedron: Polyhedron,
boundary: VEList,
twist?: Twist,
) {
const newVertexMapping = {};
_.forEach(boundary.edges, (edge, i) => {
const oppositeFace = edge.twin().face;
_.forEach(edge.vertices, (v, j) => {
_.set(
newVertexMapping,
[oppositeFace.index, v.index],
polyhedron.numVertices() + ((i + j) % boundary.numSides),
);
});
});
return polyhedron.withChanges(solid =>
solid
.addVertices(boundary.vertices)
.mapFaces(face =>
face.vertices.map(v =>
_.get(newVertexMapping, [face.index, v.index], v.index),
),
)
.addFaces(
_.flatMap(boundary.edges, edge =>
_.map(getEdgeFacePaths(edge, twist), face =>
_.map(face, path => _.get(newVertexMapping, path, path[1])),
),
),
),
);
}
示例3: duplicateVertices
function duplicateVertices(polyhedron: Polyhedron) {
const mapping: NestedRecord<number, number, number> = {};
const count = polyhedron.getVertex().adjacentFaces().length;
_.forEach(polyhedron.vertices, v => {
_.forEach(v.adjacentFaces(), (face, i) => {
_.set(mapping, [face.index, v.index], i);
});
});
return polyhedron.withChanges(solid => {
return solid
.withVertices(_.flatMap(polyhedron.vertices, v => repeat(v.value, count)))
.mapFaces(face => {
return _.flatMap(face.vertices, v => {
const base = count * v.index;
const j = mapping[face.index][v.index];
return [base + ((j + 1) % count), base + j];
});
})
.addFaces(
_.map(polyhedron.vertices, v =>
_.range(v.index * count, (v.index + 1) * count),
),
);
});
}
示例4: duplicateVertices
/**
* Duplicate the vertices, so that each face has its own unique set of vertices,
* and create a new face for each edge and new vertex set.
*/
function duplicateVertices(polyhedron: Polyhedron, twist?: Twist) {
const count = polyhedron.getVertex().adjacentFaces().length;
const newVertexMapping: NestedRecord<number, number, number> = {};
_.forEach(polyhedron.vertices, (v, vIndex: number) => {
// For each vertex, pick one adjacent face to be the "head"
// for every other adjacent face, map it to a duplicated vertex
_.forEach(v.adjacentFaces(), (f, i) => {
_.set(newVertexMapping, [f.index, v.index], v.index * count + i);
});
});
return polyhedron.withChanges(solid =>
solid
.withVertices(_.flatMap(polyhedron.vertices, v => repeat(v.value, count)))
.mapFaces(face =>
face.vertices.map(v => newVertexMapping[face.index][v.index]),
)
// Add a new face for each original vertex
.addFaces(
_.map(polyhedron.vertices, v =>
_.range(v.index * count, (v.index + 1) * count),
),
)
// Add a new face for each original edge
.addFaces(
_.flatMap(polyhedron.edges, edge =>
_.map(getEdgeFacePaths(edge, twist), face =>
_.map(face, path => _.get(newVertexMapping, path)),
),
),
),
);
}
示例5: removeExtraneousVertices
export function removeExtraneousVertices(polyhedron: Polyhedron) {
// Vertex indices to remove
const vertsInFaces = _.flatMap(polyhedron.faces, 'vertices');
const toRemove = _.filter(polyhedron.vertices, v => !v.inSet(vertsInFaces));
const numToRemove = toRemove.length;
// Map the `numToRemove` last vertices of the polyhedron (that don't overlap)
// to the first few removed vertices
const newToOld = _(polyhedron.vertices)
.takeRight(numToRemove)
.filter(v => !v.inSet(toRemove))
.map((v, i) => [v.index, toRemove[i].index])
.fromPairs()
.value();
const oldToNew = _.invert(newToOld);
const newVertices = _(polyhedron.vertices)
.map(v => polyhedron.vertices[_.get(oldToNew, v.index, v.index) as any])
.dropRight(numToRemove)
.value();
return polyhedron.withChanges(solid =>
solid
.withVertices(newVertices)
.mapFaces(face =>
_.map(face.vertices, v => _.get(newToOld, v.index, v.index)),
),
);
}
示例6: deduplicateVertices
export function deduplicateVertices(polyhedron: Polyhedron) {
// group vertex indices by same
const unique: Vertex[] = [];
const oldToNew: Record<number, number> = {};
_.forEach(polyhedron.vertices, (v, vIndex: number) => {
const match = _.find(unique, point =>
v.vec.equalsWithTolerance(point.vec, PRECISION),
);
if (match === undefined) {
unique.push(v);
oldToNew[vIndex] = vIndex;
} else {
oldToNew[vIndex] = match.index;
}
});
if (_.isEmpty(oldToNew)) return polyhedron;
// replace vertices that are the same
let newFaces = _(polyhedron.faces)
.map(face => _.uniq(face.vertices.map(v => oldToNew[v.index])))
.filter(vIndices => vIndices.length >= 3)
.value();
// remove extraneous vertices
return removeExtraneousVertices(
polyhedron.withChanges(s => s.withFaces(newFaces)),
);
}
示例7: doAugment
// Augment the following
function doAugment(
polyhedron: Polyhedron,
base: Face,
augmentType: string,
gyrate?: string,
) {
const numSides = base.numSides;
const augmentee = getAugmentee(augmentType, numSides);
const underside = augmentee.faceWithNumSides(base.numSides);
// Determine the orientations of the underside and the base
const undersideRadius = underside.vertices[0].vec
.sub(underside.centroid())
.getNormalized();
const baseIsAligned = isAligned(
polyhedron,
base,
underside,
isFastigium(augmentType, numSides) ? 'gyro' : gyrate,
augmentType,
);
const offset = baseIsAligned ? 0 : 1;
const baseRadius = base.vertices[offset].vec
.sub(base.centroid())
.getNormalized();
// https://math.stackexchange.com/questions/624348/finding-rotation-axis-and-angle-to-align-two-oriented-vectors
// Determine the transformation that rotates the underside orientation to the base orientation
// TODO we probably want this as some sort of generic method
const transformMatrix = getOrthonormalTransform(
undersideRadius,
underside.normal().getInverted(),
baseRadius,
base.normal(),
);
const transform = withOrigin(base.centroid(), u =>
transformMatrix.applyTo(u),
);
// Scale and position the augmentee so that it lines up with the base
const alignedVertices = augmentee.vertices.map(v => {
return v.vec
.sub(underside.centroid())
.scale(base.sideLength() / augmentee.edgeLength())
.add(base.centroid());
});
// Rotate the vertices so that they align with the base
const rotatedVertices = alignedVertices.map(v => transform(v));
const newAugmentee = augmentee.withChanges(solid =>
solid.withVertices(rotatedVertices).withoutFaces([underside]),
);
return polyhedron.withChanges(solid =>
solid.withoutFaces([base]).addPolyhedron(newAugmentee),
);
}
示例8: applyGyrate
function applyGyrate(polyhedron: Polyhedron, { cap }: Options) {
// get adjacent faces
const boundary = cap.boundary();
// rotate the cupola/rotunda top
const theta = TAU / boundary.numSides;
const oldToNew = mapObject(boundary.vertices, (vertex, i) => [
vertex.index,
i,
]);
const mockPolyhedron = polyhedron.withChanges(solid =>
solid.addVertices(boundary.vertices).mapFaces(face => {
if (face.inSet(cap.faces())) {
return face;
}
return face.vertices.map(v => {
return v.inSet(boundary.vertices)
? polyhedron.numVertices() + oldToNew[v.index]
: v.index;
});
}),
);
const endVertices = getTransformedVertices(
[cap],
p =>
withOrigin(p.normalRay(), v => v.getRotatedAroundAxis(p.normal(), theta)),
mockPolyhedron.vertices,
);
// TODO the animation makes the cupola shrink and expand.
// Make it not do that.
return {
animationData: {
start: mockPolyhedron,
endVertices,
},
};
}
示例9: duplicateVertices
function duplicateVertices(polyhedron: Polyhedron, facesTosharpen: Face[]) {
const offset = polyhedron.numVertices();
const mapping: NestedRecord<number, number, any> = {};
_.forEach(polyhedron.vertices, vertex => {
const v = vertex.index;
const v2 = v + offset;
const values = [v, [v2, v], v2, [v, v2]];
const faces = getShiftedAdjacentFaces(vertex, facesTosharpen);
_.forEach(faces, (f, i) => {
_.set(mapping, [f.index, v], values[i]);
});
});
// Double the amount of vertices
return polyhedron.withChanges(solid =>
solid.addVertices(polyhedron.vertices).mapFaces(f => {
return _.flatMapDeep(f.vertices, v => mapping[f.index][v.index]);
}),
);
}
示例10: bisectPrismFaces
function bisectPrismFaces(
polyhedron: Polyhedron,
boundary: VEList,
twist?: Twist,
) {
const prismFaces = _.map(boundary.edges, edge => edge.twinFace());
const newFaces = _.flatMap(boundary.edges, edge => {
const twinFace = edge.twinFace();
const [v1, v2, v3, v4] = pivot(
_.map(twinFace.vertices, 'index'),
edge.v2.index,
);
return twist === 'left'
? [[v1, v2, v4], [v2, v3, v4]]
: [[v1, v2, v3], [v1, v3, v4]];
});
return polyhedron.withChanges(solid =>
solid.withoutFaces(prismFaces).addFaces(newFaces),
);
}