Skip to content

Commit 42bb1be

Browse files
committed
Rectangular beams
1 parent a7043ae commit 42bb1be

2 files changed

Lines changed: 65 additions & 13 deletions

File tree

ressources/helpers3D.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,48 @@ function cylinderBetweenPoints(P1, P2, R1, R2, color){
4848
return [cyl, s1, s2];
4949
}
5050

51+
function rectangleBetweenPoints(P1, P2, SideA, SideB, color, SideA_dir){
52+
53+
var arr = segmentOrient(P1, P2);
54+
55+
// arr[2] = half length. Full length = 2*arr[2]
56+
var length = 2*arr[2];
57+
58+
// BoxGeometry: (X = SideA, Y = beam longitudinal axis, Z = SideB)
59+
var box_geo = new THREE.BoxGeometry(SideA, length, SideB);
60+
61+
// Extract rotation:
62+
var rotOnly = new THREE.Matrix4().extractRotation(arr[0]);
63+
// Beam longitudinal axis in global coordinates
64+
var beamAxis = new THREE.Vector3(0,1,0).applyMatrix4(rotOnly).normalize();
65+
// SideA direction in global coordinate system
66+
var SideA_global = new THREE.Vector3(1,0,0).applyMatrix4(rotOnly).normalize();
67+
// Target direction (already global and perpendicular to beam)
68+
var target = new THREE.Vector3(
69+
-SideA_dir[1], // x = -y OpenFAST
70+
SideA_dir[2], // y = z OpenFAST
71+
-SideA_dir[0] // z = -x OpenFAST
72+
).normalize();
73+
// Compute spin angle around beam axis
74+
var cross = new THREE.Vector3().crossVectors(SideA_global, target);
75+
var dot = THREE.MathUtils.clamp(SideA_global.dot(target), -1, 1);
76+
var spin_angle = Math.atan2(cross.dot(beamAxis), dot);
77+
// Apply spin angle:
78+
box_geo.rotateY(spin_angle);
79+
80+
var box_mat = new THREE.MeshPhongMaterial({
81+
color: color,
82+
shininess: 60
83+
});
84+
85+
var box = new THREE.Mesh(box_geo, box_mat);
86+
87+
box.applyMatrix4(arr[0]);
88+
box.position.copy(arr[1]);
89+
box.updateMatrixWorld();
90+
91+
return box;
92+
}
5193

5294
/** Create a Plane for Sea Level **/
5395
function createSeaLevelObject(width){
@@ -148,4 +190,4 @@ function getExtent(scene){
148190
return e;
149191
}
150192

151-
export {segmentOrient, cylinderBetweenPoints, createSeaLevelObject, createSeaBedObject, getExtent};
193+
export {segmentOrient, cylinderBetweenPoints, rectangleBetweenPoints, createSeaLevelObject, createSeaBedObject, getExtent};

ressources/script.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,33 @@ function createWorldFromJSONStream(Jstream) {
167167
// NOTE: Coord conversion OpenFAST to Three: x=-yOF, y=zOF, z=-xOF
168168
var P1 = new THREE.Vector3(-Nodes[i1][1], Nodes[i1][2], -Nodes[i1][0])
169169
var P2 = new THREE.Vector3(-Nodes[i2][1], Nodes[i2][2], -Nodes[i2][0])
170-
//console.log('Adding cylinder:',P1, P2, Props[iElem].Diam)
171-
var R = Props[iElem].Diam/2;
170+
// Color logic
171+
var color;
172172
if (Props[iElem].type==1){
173-
var color=0xc08f0e;
173+
color=0xc08f0e;
174174
} else if (Props[iElem].type==2) {
175-
var color=0x8ac00e; // cable
176-
R=R/2;
175+
color=0x8ac00e; // cable
177176
} else if (Props[iElem].type==3) {
178-
var color=0xc00e34; //rigid
177+
color=0xc00e34; //rigid
179178
} else {
180-
var color=0xeab320; //misc
179+
color=0xeab320; //misc
181180
}
182-
var arr = PLT.cylinderBetweenPoints(P1, P2, R, R, color);
183-
scene.add(arr[0]);
184-
//scene.add(arr[1]);
185-
//scene.add(arr[2]);
186-
Elems[iElem]= arr[0]; // Store cylinder
181+
var mesh; // Will hold the element to add
182+
// Shape selection for beams (cylinder or rectangle)
183+
if (Props[iElem].shape === "rectangle"){
184+
var A = Props[iElem].SideA;
185+
var B = Props[iElem].SideB;
186+
var SideA_dir = Props[iElem].SideA_dir;
187+
mesh = PLT.rectangleBetweenPoints(P1, P2, A, B, color, SideA_dir);
188+
} else {
189+
// Cylinder
190+
var R = Props[iElem].Diam/2;
191+
if (Props[iElem].type==2) { R = R/2; }
192+
var arr = PLT.cylinderBetweenPoints(P1, P2, R, R, color);
193+
mesh = arr[0]; // Use the cylinder mesh
194+
}
195+
scene.add(mesh);
196+
Elems[iElem]= mesh; // Store the element (cylinder or rectangle)
187197
}
188198

189199

0 commit comments

Comments
 (0)