Skip to content

Commit

Permalink
simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Jun 22, 2022
1 parent da9d974 commit f8ef50c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
42 changes: 30 additions & 12 deletions src/core/TypedAttributeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,49 @@ export class TypedAttributeData {

}

getGroupArray( name, index = 0 ) {
getGroupSet( index = 0 ) {

// throw an error if we've never
const { groupAttributes } = this;
const referenceAttr = groupAttributes[ 0 ][ name ];
if ( ! referenceAttr ) {
if ( groupAttributes[ index ] ) {

throw new Error( `TypedAttributeData: Attribute with "${ name }" has not been initialized` );
this.groupCount = Math.max( this.groupCount, index + 1 );
return groupAttributes[ index ];

}

// add any new group sets required
const rootAttrSet = groupAttributes[ 0 ];
this.groupCount = Math.max( this.groupCount, index + 1 );
while ( index >= groupAttributes.length ) {

groupAttributes.push( {} );
const newAttrSet = {};
groupAttributes.push( newAttrSet );
for ( const key in rootAttrSet ) {

newAttrSet[ key ] = new TypeBackedArray( rootAttrSet[ key ].type );

}

}

// initialize the array
const attributeSet = groupAttributes[ index ];
if ( ! attributeSet[ name ] ) {
return groupAttributes[ index ];

}

getGroupArray( name, index = 0 ) {

const newAttr = new TypeBackedArray( referenceAttr.type );
attributeSet[ name ] = newAttr;
// throw an error if we've never
const { groupAttributes } = this;
const rootAttrSet = groupAttributes[ 0 ];
const referenceAttr = rootAttrSet[ name ];
if ( ! referenceAttr ) {

throw new Error( `TypedAttributeData: Attribute with "${ name }" has not been initialized` );

}

return attributeSet[ name ];
return this.getGroupSet( index )[ name ];

}

Expand All @@ -80,7 +94,11 @@ export class TypedAttributeData {

} else {

rootSet[ name ] = new TypeBackedArray( type );
for ( let i = 0, l = groupAttributes.length; i < l; i ++ ) {

groupAttributes[ i ][ name ] = new TypeBackedArray( type );

}

}

Expand Down
10 changes: 6 additions & 4 deletions src/core/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function performSplitTriangleOperations( a, b, intersectionMap, operation, inver

const ia = splitIds[ i ];
const groupIndex = groupOffset === - 1 ? 0 : groupIndices[ ia ] + groupOffset;
const attrSet = attributeInfo.getGroupSet( groupIndex );

// get the triangle in the geometry B local frame
const ia3 = 3 * ia;
Expand Down Expand Up @@ -129,11 +130,11 @@ function performSplitTriangleOperations( a, b, intersectionMap, operation, inver
switch ( action ) {

case ADD_TRI:
appendAttributeFromTriangle( ia, _barycoordTri, a.geometry, a.matrixWorld, _normalMatrix, attributeInfo, groupIndex );
appendAttributeFromTriangle( ia, _barycoordTri, a.geometry, a.matrixWorld, _normalMatrix, attrSet );
break;

case INVERT_TRI:
appendAttributeFromTriangle( ia, _barycoordTri, a.geometry, a.matrixWorld, _normalMatrix, attributeInfo, groupIndex, true );
appendAttributeFromTriangle( ia, _barycoordTri, a.geometry, a.matrixWorld, _normalMatrix, attrSet, true );
break;

}
Expand Down Expand Up @@ -207,6 +208,7 @@ function performWholeTriangleOperations( a, b, splitTriSet, operation, invert, a

const currId = stack.pop();
const groupIndex = groupOffset === - 1 ? 0 : groupIndices[ currId ] + groupOffset;
const attrSet = attributeInfo.getGroupSet( groupIndex );

for ( let i = 0; i < 3; i ++ ) {

Expand All @@ -228,11 +230,11 @@ function performWholeTriangleOperations( a, b, splitTriSet, operation, invert, a
switch ( action ) {

case ADD_TRI:
appendAttributesFromIndices( i0, i1, i2, aAttributes, a.matrixWorld, _normalMatrix, attributeInfo, groupIndex );
appendAttributesFromIndices( i0, i1, i2, aAttributes, a.matrixWorld, _normalMatrix, attrSet );
break;

case INVERT_TRI:
appendAttributesFromIndices( i2, i1, i0, aAttributes, a.matrixWorld, _normalMatrix, attributeInfo, groupIndex, invert );
appendAttributesFromIndices( i2, i1, i0, aAttributes, a.matrixWorld, _normalMatrix, attrSet, invert );
break;

}
Expand Down
17 changes: 7 additions & 10 deletions src/core/operationsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export function appendAttributeFromTriangle(
matrixWorld,
normalMatrix,
attributeInfo,
groupIndex = 0,
invert = false,
) {

Expand All @@ -142,11 +141,11 @@ export function appendAttributeFromTriangle(
const i1 = indexAttr.getX( i3 + 1 );
const i2 = indexAttr.getX( i3 + 2 );

for ( const key in attributeInfo.groupAttributes[ 0 ] ) {
for ( const key in attributeInfo ) {

// check if the key we're asking for is in the geometry at all
const attr = attributes[ key ];
const arr = attributeInfo.getGroupArray( key, groupIndex );
const arr = attributeInfo[ key ];
if ( ! ( key in attributes ) ) {

throw new Error( `CSG Operations: Attribute ${ key } not available on geometry.` );
Expand Down Expand Up @@ -203,13 +202,12 @@ export function appendAttributesFromIndices(
matrixWorld,
normalMatrix,
attributeInfo,
groupIndex = 0,
invert = false,
) {

appendAttributeFromIndex( i0, attributes, matrixWorld, normalMatrix, attributeInfo, groupIndex, invert );
appendAttributeFromIndex( i1, attributes, matrixWorld, normalMatrix, attributeInfo, groupIndex, invert );
appendAttributeFromIndex( i2, attributes, matrixWorld, normalMatrix, attributeInfo, groupIndex, invert );
appendAttributeFromIndex( i0, attributes, matrixWorld, normalMatrix, attributeInfo, invert );
appendAttributeFromIndex( i1, attributes, matrixWorld, normalMatrix, attributeInfo, invert );
appendAttributeFromIndex( i2, attributes, matrixWorld, normalMatrix, attributeInfo, invert );

}

Expand Down Expand Up @@ -338,15 +336,14 @@ function appendAttributeFromIndex(
matrixWorld,
normalMatrix,
attributeInfo,
groupIndex = 0,
invert = false,
) {

for ( const key in attributeInfo.groupAttributes[ 0 ] ) {
for ( const key in attributeInfo ) {

// check if the key we're asking for is in the geometry at all
const attr = attributes[ key ];
const arr = attributeInfo.getGroupArray( key, groupIndex );
const arr = attributeInfo[ key ];
if ( ! ( key in attributes ) ) {

throw new Error( `CSG Operations: Attribute ${ key } no available on geometry.` );
Expand Down

0 comments on commit f8ef50c

Please sign in to comment.