Skip to content

Commit

Permalink
Merge pull request #5 from polyhobbyist/handle_complex
Browse files Browse the repository at this point in the history
Handle complex
  • Loading branch information
polyhobbyist authored Sep 25, 2023
2 parents ea736e2 + b589a79 commit 59fe9ef
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 55 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions src/GeometryBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Box implements IGeometry {
public depth : number = 0;


public mesh: BABYLON.Mesh | undefined = undefined;
public meshes: BABYLON.Mesh[] = [];
public transform : BABYLON.TransformNode | undefined;

constructor(x : number, y: number, z: number) {
Expand All @@ -26,21 +26,25 @@ export class Box implements IGeometry {
public create(scene: BABYLON.Scene, mat : Material | undefined) : void {
this.transform = new BABYLON.TransformNode("mesh_box", scene);

this.mesh = BABYLON.MeshBuilder.CreateBox("box",
this.meshes.push(BABYLON.MeshBuilder.CreateBox("box",
{
width: this.width,
height: this.height,
depth: this.depth,
}, scene);
}, scene));

this.mesh.parent = this.transform;
this.meshes[0].parent = this.transform;
if (mat != undefined && mat.material != undefined) {
this.mesh.material = mat.material;
this.meshes[0].material = mat.material;
}
}

public dispose() : void {
this.mesh?.dispose();
if (this.meshes != undefined) {
this.meshes.forEach(m => {
m.dispose();
});
}
this.transform?.dispose();
}

Expand Down
18 changes: 11 additions & 7 deletions src/GeometryCylinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Cylinder implements IGeometry {
public radius : number = 0;


public mesh: BABYLON.AbstractMesh | undefined = undefined;
public meshes: BABYLON.AbstractMesh[] = [];
public transform : BABYLON.TransformNode | undefined;

constructor(l : number, r: number) {
Expand All @@ -18,20 +18,24 @@ export class Cylinder implements IGeometry {
public create(scene: BABYLON.Scene, mat : Material | undefined) : void {
this.transform = new BABYLON.TransformNode("mesh_cylinder", scene);

this.mesh = BABYLON.MeshBuilder.CreateCylinder("cylinder",
this.meshes.push(BABYLON.MeshBuilder.CreateCylinder("cylinder",
{
diameter: this.radius * 2.0,
height: this.length
}, scene);
}, scene));

this.mesh.parent = this.transform;
this.mesh.addRotation(Math.PI / 2.0, 0, 0);
this.meshes[0].parent = this.transform;
this.meshes[0].addRotation(Math.PI / 2.0, 0, 0);
if (mat != undefined && mat.material != undefined) {
this.mesh.material = mat.material;
this.meshes[0].material = mat.material;
}
}
public dispose() : void {
this.mesh?.dispose();
if (this.meshes != undefined) {
this.meshes.forEach(m => {
m.dispose();
});
}
this.transform?.dispose();
}
}
48 changes: 33 additions & 15 deletions src/GeometryMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class Mesh implements IGeometry {
public uri: string = "";
public scale: BABYLON.Vector3 = new BABYLON.Vector3(1.0, 1.0, 1.0);

public mesh: BABYLON.AbstractMesh | undefined = undefined;
public meshes: BABYLON.AbstractMesh[] | undefined = undefined;
public transform : BABYLON.TransformNode | undefined = undefined;
public material : Material | undefined = undefined;

Expand All @@ -18,50 +18,68 @@ export class Mesh implements IGeometry {
this.scale = scale;
}

private meshCallback(mesh : BABYLON.AbstractMesh[]) {
private meshCallback(scene: BABYLON.Scene, meshes : BABYLON.AbstractMesh[], particleSystems : BABYLON.IParticleSystem[] | undefined, skeletons : BABYLON.Skeleton[] | undefined) {
// Get a pointer to the mesh
if (mesh.length > 0) {
this.mesh = mesh[0];
if (this.mesh != null &&
this.transform != undefined) {
this.mesh.parent = this.transform;
this.mesh.scaling = this.scale;
this.mesh.addRotation(0, 0, Math.PI).addRotation(Math.PI/2, 0, 0);
if (this.material != undefined && this.material.material != undefined) {
this.mesh.material = this.material.material;
if (meshes.length > 0 && this.transform != undefined) {
this.meshes = meshes;
this.meshes[0].parent = this.transform;

// find the top level bone in skeletons
if (skeletons != undefined && skeletons.length > 0) {
let rootBone = skeletons[0].bones.find(b => b.getParent() == undefined);
if (rootBone != undefined) {
rootBone.returnToRest();
}
} else {

this.meshes.forEach(m => {
if (this.transform != undefined) {
m.addRotation(0, 0, Math.PI).addRotation(Math.PI/2, 0, 0);
m.parent = this.transform;

if (this.material != undefined && this.material.material != undefined) {
m.material = this.material.material;
}
}
});
}
}
}


public create(scene: BABYLON.Scene, mat : Material | undefined) : void {
this.transform = new BABYLON.TransformNode("mesh_mesh", scene);
this.transform.scaling = this.scale;

this.material = mat;

if (this.uri.startsWith("file://"))
{
// Handle relative paths
var filePath = this.uri.substring(7);
var filePath = this.uri.substring(7);
if (!filePath.startsWith("/")) {
filePath = path.join(__dirname, filePath);
}
var fileExtension = filePath.substring(filePath.lastIndexOf('.'));
var meshdata = readFileSync(filePath).toString('base64');

// Force the file to be read as base64 encoded data blob
BABYLON.SceneLoader.ImportMesh(null, "", "data:;base64," + meshdata, scene, (mesh) => {this.meshCallback(mesh)}, null, null, fileExtension);
BABYLON.SceneLoader.ImportMesh(null, "", "data:;base64," + meshdata, scene, (mesh, ps, sk) => {this.meshCallback(scene, mesh, ps, sk)}, null, null, fileExtension);
} else {
let filename = this.uri.substring(this.uri.lastIndexOf('/') + 1);
if (filename) {
let base = this.uri.substring(0, this.uri.lastIndexOf('/') + 1);
BABYLON.SceneLoader.ImportMesh(null, base, filename, scene, (mesh) => {this.meshCallback(mesh)});
BABYLON.SceneLoader.ImportMesh(null, base, filename, scene, (mesh, ps, sk) => {this.meshCallback(scene, mesh, ps, sk)});
}
}
}

public dispose(): void {
this.mesh?.dispose();
if (this.meshes != undefined) {
this.meshes.forEach(m => {
m.dispose();
});
}
this.transform?.dispose();
}
}
16 changes: 10 additions & 6 deletions src/GeometrySphere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Sphere implements IGeometry {
public radius : number = 0;


public mesh: BABYLON.AbstractMesh | undefined = undefined;
public meshes: BABYLON.AbstractMesh[] = [];
public transform : BABYLON.TransformNode | undefined;

constructor(r: number) {
Expand All @@ -17,19 +17,23 @@ export class Sphere implements IGeometry {
public create(scene: BABYLON.Scene, mat : Material | undefined) : void {
this.transform = new BABYLON.TransformNode("mesh_sphere", scene);

this.mesh = BABYLON.MeshBuilder.CreateSphere("sphere",
this.meshes.push(BABYLON.MeshBuilder.CreateSphere("sphere",
{
diameter: this.radius * 2.0,
}, scene);
}, scene));

this.mesh.parent = this.transform;
this.meshes[0].parent = this.transform;
if (mat != undefined && mat.material != undefined) {
this.mesh.material = mat.material;
this.meshes[0].material = mat.material;
}
}

public dispose() : void {
this.mesh?.dispose();
if (this.meshes != undefined) {
this.meshes.forEach(m => {
m.dispose();
});
}
this.transform?.dispose();
}
}
2 changes: 1 addition & 1 deletion src/IGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as BABYLON from 'babylonjs';
import { Material } from './Material';

export interface IGeometry {
mesh : BABYLON.AbstractMesh | undefined;
meshes : BABYLON.AbstractMesh[] | undefined;
transform : BABYLON.TransformNode | undefined;

create(scene: BABYLON.Scene, mat : Material | undefined) : void;
Expand Down
23 changes: 14 additions & 9 deletions src/Render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Materials from 'babylonjs-materials';
import * as urdf from './urdf';
import {Robot} from './Robot';
import * as GUI from 'babylonjs-gui';
import * as ColladaFileLoader from '@polyhobbyist/babylon-collada-loader';

var statusLabel = new GUI.TextBlock();
let axisList : BABYLON.PositionGizmo[] = [];
Expand Down Expand Up @@ -99,18 +100,22 @@ function toggleAxisRotationOnRobot(ui: GUI.AdvancedDynamicTexture, scene : BABYL

var createScene = async function (engine : BABYLON.Engine, canvas : HTMLCanvasElement) : Promise<BABYLON.Scene>{
let scene = new BABYLON.Scene(engine);
if (BABYLON.SceneLoader) {
//Add this loader into the register plugin
BABYLON.SceneLoader.RegisterPlugin(new ColladaFileLoader.DAEFileLoader());
}

scene.useRightHandedSystem = true;
scene.clearColor = BABYLON.Color4.FromColor3(BABYLON.Color3.Black());// TODO (polyhobbyist) Make this configurable
scene.clearColor = new BABYLON.Color4(0.4, 0.4, 0.4, 1.0);// TODO (polyhobbyist) Make this configurable

var radius = 1; // TODO (polyhobbyist): make this configurable
var radius = 10; // TODO (polyhobbyist): make this configurable

// This creates and positions a free camera (non-mesh)
var camera = new BABYLON.ArcRotateCamera("camera1", - Math.PI / 3, 5 * Math.PI / 12, radius, new BABYLON.Vector3(0, 0, 0), scene);
camera.wheelDeltaPercentage = 0.01;
camera.minZ = 0.1;

const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 20, 20), scene);

// This attaches the camera to the canvas
camera.attachControl(canvas, true);
Expand Down Expand Up @@ -229,7 +234,7 @@ export async function RenderMain() {
<joint name="dome_to_camera_joint" type="fixed">
<parent link="base_link"/>
<child link="camera_link"/>
<origin xyz="0 0.12 0.08" rpy="-1.047198 -0.125826 -3.047137"/>
<origin xyz="0 0.12 0.08" rpy="2.093 0 0"/>
</joint>
<link name="front_depth_link">
Expand All @@ -244,7 +249,7 @@ export async function RenderMain() {
<joint name="dome_to_front_depth_joint" type="fixed">
<parent link="base_link"/>
<child link="front_depth_link"/>
<origin xyz="0 0.11 0.09" rpy="-1.047198 0 3.14159"/>
<origin xyz="0 0.11 0.09" rpy="2.093 0 0"/>
</joint>
<link name="left_depth_link">
Expand All @@ -259,7 +264,7 @@ export async function RenderMain() {
<joint name="dome_to_left_depth_joint" type="fixed">
<parent link="base_link"/>
<child link="left_depth_link"/>
<origin xyz="0.122 -0.0715 0.008" rpy="1.15863 1.74874 0"/>
<origin xyz="0.11693 -0.067 0.05" rpy="1.047198 0.959931 0.436332"/>
</joint>
<link name="right_depth_link">
Expand All @@ -274,7 +279,7 @@ export async function RenderMain() {
<joint name="dome_to_right_depth_joint" type="fixed">
<parent link="base_link"/>
<child link="right_depth_link"/>
<origin xyz="-0.122 -0.0715 0.008" rpy="0.49281 -1.51029 3.14159"/>
<origin xyz="-0.11693 -0.067 0.05" rpy="1.047198 -0.959931 -0.436332"/>
</joint>
Expand All @@ -293,12 +298,12 @@ export async function RenderMain() {
</joint>
</robot>
`;
`;

const canvas = document.getElementById("renderCanvas") as HTMLCanvasElement; // Get the canvas element
const engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
let scene = await createScene(engine, canvas);
//scene.debugLayer.show();
scene.debugLayer.show();

let robot = await applyURDF(scene, u);

Expand Down
2 changes: 1 addition & 1 deletion test/testdata/basic_with_stl_mesh.urdf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<link name="base_link">
<visual>
<geometry>
<mesh filename="file://../test/testdata/xyz_cube.stl" />
<mesh filename="https://raw.githubusercontent.com/polyhobbyist/babylon_ros/ea736e27ccd4df44096bc5c05bacaf5171c6b86a/test/testdata/xyz_cybe.stl" />
</geometry>
</visual>
</link>
Expand Down
12 changes: 4 additions & 8 deletions test/testdata/r2.urdf
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@
<visual>
<origin rpy="0.0 0 0" xyz="0 0 0"/>
<geometry>
<!-- mesh filename="package://urdf_tutorial/meshes/l_finger.dae"/ -->
<cylinder length="0.1" radius="0.035"/>
<mesh filename="https://raw.githubusercontent.com/ros/urdf_tutorial/ros1/meshes/l_finger.dae"/>
</geometry>
</visual>
</link>
Expand All @@ -184,8 +183,7 @@
<visual>
<origin rpy="0.0 0 0" xyz="0.09137 0.00495 0"/>
<geometry>
<!-- mesh filename="package://urdf_tutorial/meshes/l_finger_tip.dae"/ -->
<cylinder length="0.1" radius="0.035"/>
<mesh filename="https://raw.githubusercontent.com/ros/urdf_tutorial/ros1/meshes/l_finger_tip.dae"/>
</geometry>
</visual>
</link>
Expand All @@ -199,8 +197,7 @@
<visual>
<origin rpy="-3.1415 0 0" xyz="0 0 0"/>
<geometry>
<!-- mesh filename="package://urdf_tutorial/meshes/l_finger.dae"/ -->
<cylinder length="0.1" radius="0.035"/>
<mesh filename="https://raw.githubusercontent.com/ros/urdf_tutorial/ros1/meshes/l_finger.dae"/>
</geometry>
</visual>
</link>
Expand All @@ -214,8 +211,7 @@
<visual>
<origin rpy="-3.1415 0 0" xyz="0.09137 0.00495 0"/>
<geometry>
<!-- mesh filename="package://urdf_tutorial/meshes/l_finger_tip.dae"/ -->
<cylinder length="0.1" radius="0.035"/>
<mesh filename="https://raw.githubusercontent.com/ros/urdf_tutorial/ros1/meshes/l_finger_tip.dae"/>
</geometry>
</visual>
</link>
Expand Down

0 comments on commit 59fe9ef

Please sign in to comment.