From 58ebeb2baac48f9066c1395a9071f99745574ef9 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Mon, 8 Apr 2019 18:48:28 +0300 Subject: [PATCH] Mesh API to use Tuple3d instead of Vector3d Added ArcCylinder for creating cylinder shapes bend on an arc of circle Change-Id: I09713df59e7cbf0d6805f055a039af46d57934d6 --- .../simantics/g3d/vtk/shape/MeshActor.java | 195 ++++----- .../org/simantics/g3d/vtk/utils/vtkUtil.java | 266 +++++++------ .../org/simantics/g3d/shape/ArcCylinder.java | 152 +++++++ .../src/org/simantics/g3d/shape/Box.java | 192 ++++----- .../src/org/simantics/g3d/shape/Cone.java | 150 +++---- .../src/org/simantics/g3d/shape/Cylinder.java | 69 ++-- .../src/org/simantics/g3d/shape/Mesh.java | 312 ++++++++------- .../src/org/simantics/g3d/shape/Sphere.java | 210 +++++----- .../src/org/simantics/g3d/shape/Tube.java | 376 +++++++++--------- 9 files changed, 1053 insertions(+), 869 deletions(-) create mode 100644 org.simantics.g3d/src/org/simantics/g3d/shape/ArcCylinder.java diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java index 042142fc..0cfc8246 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java @@ -1,97 +1,98 @@ -/******************************************************************************* - * Copyright (c) 2012, 2013 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.g3d.vtk.shape; - -import javax.vecmath.Vector3d; - -import org.simantics.g3d.shape.Color4d; -import org.simantics.g3d.shape.Mesh; - -import vtk.vtkActor; -import vtk.vtkDataSetMapper; -import vtk.vtkIdList; -import vtk.vtkPoints; -import vtk.vtkPolyData; -import vtk.vtkPolyDataMapper; -import vtk.vtkPolyDataNormals; -import vtk.vtkTriangle; -import vtk.vtkUnsignedCharArray; - -public class MeshActor extends vtkActor { - - public void setMesh(Mesh mesh) { - - vtkPolyDataMapper mapper = new vtkPolyDataMapper(); - - vtkPolyData polyData = new vtkPolyData(); - polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3); - - vtkTriangle triangle = new vtkTriangle(); - vtkIdList list = triangle.GetPointIds(); - for (int i = 0; i < mesh.getIndices().size(); i+=3) { - list.SetId(0, mesh.getIndices().get(i)); - list.SetId(1, mesh.getIndices().get(i+1)); - list.SetId(2, mesh.getIndices().get(i+2)); - polyData.InsertNextCell(triangle.GetCellType(), list); - } - list.Delete(); - triangle.Delete(); - - - vtkPoints points = new vtkPoints(); - for (int i = 0; i < mesh.getVertices().size(); i++) { - Vector3d p = mesh.getVertices().get(i); - points.InsertPoint(i, p.x, p.y, p.z); - } - - polyData.SetPoints(points); - points.Delete(); - - if (mesh.getColors() != null) { - vtkUnsignedCharArray colors = new vtkUnsignedCharArray(); - colors.SetName("Colors"); - colors.SetNumberOfComponents(3); - colors.SetNumberOfTuples(mesh.getColors().size()); - for (int i = 0; i < mesh.getColors().size(); i++) { - Color4d c = mesh.getColors().get(i); - colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z); - } - polyData.GetPointData().AddArray(colors); - colors.Delete(); - - } - - boolean computeNormals = true; - if (computeNormals) { - vtkPolyDataNormals normals = new vtkPolyDataNormals(); - normals.SetInput(polyData); - mapper.SetInputConnection(normals.GetOutputPort()); - normals.GetOutputPort().Delete(); - normals.Delete(); - } else { - mapper.SetInput(polyData); - } - - if (mesh.getColors() != null) { - mapper.ScalarVisibilityOn(); - mapper.SetScalarModeToUsePointFieldData(); - mapper.SelectColorArray("Colors"); - } - - SetMapper(mapper); - mapper.Delete(); - polyData.GetPointData().Delete(); - polyData.Delete(); - - } - -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.vtk.shape; + +import javax.vecmath.Tuple3d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.shape.Color4d; +import org.simantics.g3d.shape.Mesh; + +import vtk.vtkActor; +import vtk.vtkDataSetMapper; +import vtk.vtkIdList; +import vtk.vtkPoints; +import vtk.vtkPolyData; +import vtk.vtkPolyDataMapper; +import vtk.vtkPolyDataNormals; +import vtk.vtkTriangle; +import vtk.vtkUnsignedCharArray; + +public class MeshActor extends vtkActor { + + public void setMesh(Mesh mesh) { + + vtkPolyDataMapper mapper = new vtkPolyDataMapper(); + + vtkPolyData polyData = new vtkPolyData(); + polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3); + + vtkTriangle triangle = new vtkTriangle(); + vtkIdList list = triangle.GetPointIds(); + for (int i = 0; i < mesh.getIndices().size(); i+=3) { + list.SetId(0, mesh.getIndices().get(i)); + list.SetId(1, mesh.getIndices().get(i+1)); + list.SetId(2, mesh.getIndices().get(i+2)); + polyData.InsertNextCell(triangle.GetCellType(), list); + } + list.Delete(); + triangle.Delete(); + + + vtkPoints points = new vtkPoints(); + for (int i = 0; i < mesh.getVertices().size(); i++) { + Tuple3d p = mesh.getVertices().get(i); + points.InsertPoint(i, p.x, p.y, p.z); + } + + polyData.SetPoints(points); + points.Delete(); + + if (mesh.getColors() != null) { + vtkUnsignedCharArray colors = new vtkUnsignedCharArray(); + colors.SetName("Colors"); + colors.SetNumberOfComponents(3); + colors.SetNumberOfTuples(mesh.getColors().size()); + for (int i = 0; i < mesh.getColors().size(); i++) { + Color4d c = mesh.getColors().get(i); + colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z); + } + polyData.GetPointData().AddArray(colors); + colors.Delete(); + + } + + boolean computeNormals = true; + if (computeNormals) { + vtkPolyDataNormals normals = new vtkPolyDataNormals(); + normals.SetInput(polyData); + mapper.SetInputConnection(normals.GetOutputPort()); + normals.GetOutputPort().Delete(); + normals.Delete(); + } else { + mapper.SetInput(polyData); + } + + if (mesh.getColors() != null) { + mapper.ScalarVisibilityOn(); + mapper.SetScalarModeToUsePointFieldData(); + mapper.SelectColorArray("Colors"); + } + + SetMapper(mapper); + mapper.Delete(); + polyData.GetPointData().Delete(); + polyData.Delete(); + + } + +} diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkUtil.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkUtil.java index 6a9b6b91..71710def 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkUtil.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkUtil.java @@ -1,130 +1,136 @@ -/******************************************************************************* - * Copyright (c) 2012, 2013 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.g3d.vtk.utils; - -import java.util.Collection; - -import javax.vecmath.AxisAngle4d; -import javax.vecmath.Matrix4d; -import javax.vecmath.Point2d; -import javax.vecmath.Point3d; -import javax.vecmath.Quat4d; -import javax.vecmath.Tuple3d; -import javax.vecmath.Vector3d; - -import org.simantics.g3d.math.MathTools; -import org.simantics.g3d.math.Ray; - -import vtk.vtkMatrix4x4; -import vtk.vtkProp3D; -import vtk.vtkRenderer; - -public class vtkUtil { - - public static Ray createMouseRay(vtkRenderer ren1, double x, double y) { - Point2d screenPos = new Point2d(x,y); - Point3d worldCoords = getWorldCoordinates(ren1, screenPos, 0); - Point3d worldCoords2 = getWorldCoordinates(ren1, screenPos, 1); - Vector3d dir = new Vector3d(worldCoords2); - dir.sub(worldCoords); - return new Ray(worldCoords, dir); - } - - public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) { - - ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos); - ren1.DisplayToWorld(); - double world[] = ren1.GetWorldPoint(); - - return new Point3d(world); - - } - - public static Point2d getScreenCoordinates(vtkRenderer ren1, Tuple3d worldPos) { - ren1.SetWorldPoint(worldPos.x, worldPos.y, worldPos.z, 0.0); - ren1.WorldToDisplay(); - double screen[] = ren1.GetDisplayPoint(); - - return new Point2d(screen); - - } - - public static Matrix4d getMatrix(vtkMatrix4x4 ptm) { - Matrix4d mat = new Matrix4d(); - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - mat.setElement(i, j, ptm.GetElement(i, j)); - } - } - - return mat; - } - - public static vtkMatrix4x4 getMatrix(Matrix4d m) { - vtkMatrix4x4 mat= new vtkMatrix4x4(); - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - mat.SetElement(i, j, m.getElement(i, j)); - } - } - return mat; - } - - public static void updateTransform(Collection props, Vector3d pos, Quat4d q) { - AxisAngle4d aa = new AxisAngle4d(); - aa.set(q); - updateTransform(props, pos, aa); - } - - public static void updateTransform(vtkProp3D actor, double pos[], AxisAngle4d aa) { - actor.SetOrientation(0, 0, 0); - actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - actor.SetPosition(pos); - } - - public static void updateTransform(vtkProp3D actor, AxisAngle4d aa) { - actor.SetOrientation(0, 0, 0); - actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - } - - - public static void updateTransform(Collection props, Vector3d pos, AxisAngle4d aa) { - for (vtkProp3D actor : props) { - actor.SetOrientation(0, 0, 0); - actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - actor.SetPosition(pos.x, pos.y, pos.z); - } - } - - public static void updateTransform(Collection props, Vector3d pos, AxisAngle4d aa, double scale) { - for (vtkProp3D actor : props) { - actor.SetOrientation(0, 0, 0); - actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - actor.SetScale(scale); - actor.SetPosition(pos.x,pos.y,pos.z); - } - } - - public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scale) { - actor.SetOrientation(0, 0, 0); - actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - actor.SetScale(scale); - actor.SetPosition(pos.x,pos.y,pos.z); - } - - public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scalex, double scaley, double scalez) { - actor.SetOrientation(0, 0, 0); - actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); - actor.SetScale(scalex,scaley, scalez); - actor.SetPosition(pos.x,pos.y,pos.z); - } -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.vtk.utils; + +import java.util.Collection; + +import javax.vecmath.AxisAngle4d; +import javax.vecmath.Matrix4d; +import javax.vecmath.Point2d; +import javax.vecmath.Point3d; +import javax.vecmath.Quat4d; +import javax.vecmath.Tuple3d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.math.MathTools; +import org.simantics.g3d.math.Ray; + +import vtk.vtkMatrix4x4; +import vtk.vtkProp3D; +import vtk.vtkRenderer; + +public class vtkUtil { + + public static Ray createMouseRay(vtkRenderer ren1, double x, double y) { + Point2d screenPos = new Point2d(x,y); + Point3d worldCoords = getWorldCoordinates(ren1, screenPos, 0); + Point3d worldCoords2 = getWorldCoordinates(ren1, screenPos, 1); + Vector3d dir = new Vector3d(worldCoords2); + dir.sub(worldCoords); + return new Ray(worldCoords, dir); + } + + public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) { + + ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos); + ren1.DisplayToWorld(); + double world[] = ren1.GetWorldPoint(); + + return new Point3d(world); + + } + + public static Point2d getScreenCoordinates(vtkRenderer ren1, Tuple3d worldPos) { + ren1.SetWorldPoint(worldPos.x, worldPos.y, worldPos.z, 0.0); + ren1.WorldToDisplay(); + double screen[] = ren1.GetDisplayPoint(); + + return new Point2d(screen); + + } + + public static Matrix4d getMatrix(vtkMatrix4x4 ptm) { + Matrix4d mat = new Matrix4d(); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + mat.setElement(i, j, ptm.GetElement(i, j)); + } + } + + return mat; + } + + public static vtkMatrix4x4 getMatrix(Matrix4d m) { + vtkMatrix4x4 mat= new vtkMatrix4x4(); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + mat.SetElement(i, j, m.getElement(i, j)); + } + } + return mat; + } + + public static void updateTransform(Collection props, Vector3d pos, Quat4d q) { + AxisAngle4d aa = new AxisAngle4d(); + aa.set(q); + updateTransform(props, pos, aa); + } + + public static void updateTransform(vtkProp3D actor, double pos[], AxisAngle4d aa) { + actor.SetOrientation(0, 0, 0); + actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + actor.SetPosition(pos); + } + + public static void updateTransform(vtkProp3D actor, Tuple3d pos, AxisAngle4d aa) { + actor.SetOrientation(0, 0, 0); + actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + actor.SetPosition(new double[] {pos.x,pos.y,pos.z}); + } + + public static void updateTransform(vtkProp3D actor, AxisAngle4d aa) { + actor.SetOrientation(0, 0, 0); + actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + } + + + public static void updateTransform(Collection props, Vector3d pos, AxisAngle4d aa) { + for (vtkProp3D actor : props) { + actor.SetOrientation(0, 0, 0); + actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + actor.SetPosition(pos.x, pos.y, pos.z); + } + } + + public static void updateTransform(Collection props, Vector3d pos, AxisAngle4d aa, double scale) { + for (vtkProp3D actor : props) { + actor.SetOrientation(0, 0, 0); + actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + actor.SetScale(scale); + actor.SetPosition(pos.x,pos.y,pos.z); + } + } + + public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scale) { + actor.SetOrientation(0, 0, 0); + actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + actor.SetScale(scale); + actor.SetPosition(pos.x,pos.y,pos.z); + } + + public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scalex, double scaley, double scalez) { + actor.SetOrientation(0, 0, 0); + actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z); + actor.SetScale(scalex,scaley, scalez); + actor.SetPosition(pos.x,pos.y,pos.z); + } +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/shape/ArcCylinder.java b/org.simantics.g3d/src/org/simantics/g3d/shape/ArcCylinder.java new file mode 100644 index 00000000..ab617844 --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/g3d/shape/ArcCylinder.java @@ -0,0 +1,152 @@ +package org.simantics.g3d.shape; + +import java.util.ArrayList; +import java.util.List; + +import javax.vecmath.AxisAngle4d; +import javax.vecmath.Point3d; +import javax.vecmath.Quat4d; +import javax.vecmath.Tuple3d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.math.MathTools; + +public class ArcCylinder { + + public Mesh create(Point3d s, Point3d v, Point3d e, double rad, int res) { + + Vector3d v1 = new Vector3d(s); + v1.sub(v); + + Vector3d v2 = new Vector3d(e); + v2.sub(v); + + double a = v2.angle(v1); + int steps = 0; + double sa = 0.0; + + Vector3d rn = new Vector3d(); + Vector3d r1 = null; + Vector3d c = null; + + if ((a +0.0001) > Math.PI) { + steps = 1; + } else { + c = new Vector3d(v2); + c.add(v1); + c.normalize(); + c.scale(v1.length() * (1.0/Math.cos(a*0.5))); + c.add(v); + + r1 = new Vector3d(s); + r1.sub(c); + + Vector3d r2 = new Vector3d(e); + r2.sub(c); + + a = r2.angle(r1); + + rn.cross(v2, v1); + rn.normalize(); + + steps = (int)(Math.ceil(a/0.1)); + if (steps == 0) + steps = 1; + sa = a/steps; + } + + + List vertices = new ArrayList(res * (steps+1)); + List normals = new ArrayList(res * (steps+1)); + List indices = new ArrayList(); + + for (int i = 0; i <= steps; i++) { + Vector3d p; + Vector3d t; + if (i == 0) { + p = new Vector3d(s); + t = new Vector3d(v1); + t.negate(); + t.normalize(); + } else if (i == steps) { + p = new Vector3d(e); + t = new Vector3d(v2); + t.normalize(); + } else { + p = new Vector3d(); + double ca = sa * i; + Quat4d q = MathTools.getQuat(new AxisAngle4d(rn, ca)); + MathTools.rotate(q, r1, p); + t = new Vector3d(); + t.cross(rn,p); + t.normalize(); + p.add(c); + + } + createCircle(vertices, normals, p, t, rn, res, rad); + } + int count = steps*res*6; + for (int i = 0; i < count; i++) { + indices.add(-1); + } + createIndices(steps, res, indices); + return new Mesh(vertices, normals, indices); + } + + private static void createCircle(List points, List normals, Tuple3d p, Vector3d t, Vector3d n, int res, double radius) { + n = new Vector3d(n); + n.scale(radius); + + for (int index = 0; index < res; index ++) { + Vector3d v; + if (index == 0) { + v = new Vector3d(n); + + } else { + AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)res); + v = new Vector3d(); + MathTools.rotate(MathTools.getQuat(aa), n, v); + } + //int vIndex = (i*resolution + index)*3; + Vector3d pt = new Vector3d(p); + pt.add(v); + //points.set(vIndex, pt); + points.add(pt); + v.normalize(); + //normals.set(vIndex, v); + normals.add(v); + } + } + private static void createIndices(int steps, int resolution, List index) { + for (int c = 0; c < steps; c++) { + for (int s = 0; s < resolution; s++) { + int ii = (c * resolution + s) * 6; + int iv = c*resolution + s; + + /* + iv+1 ---- iv + resolution + 1 + | /| + |/ | + iv ---- iv + resolution + */ + if (s < resolution - 1) { + index.set(ii+2,iv); + index.set(ii+1,iv+resolution); + index.set(ii+0,iv+resolution+1); + + index.set(ii+5,iv); + index.set(ii+4,iv+resolution+1); + index.set(ii+3,iv+1); + } else { + index.set(ii+2,iv); + index.set(ii+1,iv+resolution); + index.set(ii+0,iv+1); + + index.set(ii+5,iv); + index.set(ii+4,iv+1); + index.set(ii+3,iv+1-resolution); + } + } + } + } +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/shape/Box.java b/org.simantics.g3d/src/org/simantics/g3d/shape/Box.java index 6fab2b80..fa716b74 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/shape/Box.java +++ b/org.simantics.g3d/src/org/simantics/g3d/shape/Box.java @@ -1,96 +1,96 @@ -package org.simantics.g3d.shape; - -import java.util.ArrayList; -import java.util.List; - -import javax.vecmath.Vector3d; - -public class Box { - - Vector3d min; - Vector3d max; - - public Vector3d getMin() { - return min; - } - public Vector3d getMax() { - return max; - } - - public Box(Vector3d min, Vector3d max) { - this.max = max; - this.min = min; - } - - public Box(double minx, double miny, double minz, double maxx, double maxy, double maxz) { - min = new Vector3d(minx, miny, minz); - max = new Vector3d(maxx, maxy, maxz); - } - - public Box(double min[], double max[]) { - this.min = new Vector3d(min); - this.max = new Vector3d(max); - } - - public Mesh createMesh() { - List vertices = new ArrayList(); - vertices.add(new Vector3d(min.x, min.y, min.z)); - vertices.add(new Vector3d(min.x, min.y, max.z)); - vertices.add(new Vector3d(min.x, max.y, min.z)); - vertices.add(new Vector3d(min.x, max.y, max.z)); - vertices.add(new Vector3d(max.x, min.y, min.z)); - vertices.add(new Vector3d(max.x, min.y, max.z)); - vertices.add(new Vector3d(max.x, max.y, min.z)); - vertices.add(new Vector3d(max.x, max.y, max.z)); - List indices = new ArrayList(); - indices.add(0); - indices.add(2); - indices.add(1); - - indices.add(1); - indices.add(2); - indices.add(3); - - indices.add(2); - indices.add(6); - indices.add(3); - - indices.add(3); - indices.add(6); - indices.add(7); - - indices.add(5); - indices.add(1); - indices.add(7); - - indices.add(1); - indices.add(3); - indices.add(7); - - indices.add(4); - indices.add(5); - indices.add(6); - - indices.add(5); - indices.add(7); - indices.add(6); - - indices.add(0); - indices.add(4); - indices.add(2); - - indices.add(2); - indices.add(4); - indices.add(6); - - indices.add(0); - indices.add(1); - indices.add(4); - - indices.add(5); - indices.add(4); - indices.add(1); - return new Mesh(vertices, indices); - } - -} +package org.simantics.g3d.shape; + +import java.util.ArrayList; +import java.util.List; + +import javax.vecmath.Vector3d; + +public class Box { + + Vector3d min; + Vector3d max; + + public Vector3d getMin() { + return min; + } + public Vector3d getMax() { + return max; + } + + public Box(Vector3d min, Vector3d max) { + this.max = max; + this.min = min; + } + + public Box(double minx, double miny, double minz, double maxx, double maxy, double maxz) { + min = new Vector3d(minx, miny, minz); + max = new Vector3d(maxx, maxy, maxz); + } + + public Box(double min[], double max[]) { + this.min = new Vector3d(min); + this.max = new Vector3d(max); + } + + public Mesh createMesh() { + List vertices = new ArrayList(); + vertices.add(new Vector3d(min.x, min.y, min.z)); + vertices.add(new Vector3d(min.x, min.y, max.z)); + vertices.add(new Vector3d(min.x, max.y, min.z)); + vertices.add(new Vector3d(min.x, max.y, max.z)); + vertices.add(new Vector3d(max.x, min.y, min.z)); + vertices.add(new Vector3d(max.x, min.y, max.z)); + vertices.add(new Vector3d(max.x, max.y, min.z)); + vertices.add(new Vector3d(max.x, max.y, max.z)); + List indices = new ArrayList(); + indices.add(0); + indices.add(2); + indices.add(1); + + indices.add(1); + indices.add(2); + indices.add(3); + + indices.add(2); + indices.add(6); + indices.add(3); + + indices.add(3); + indices.add(6); + indices.add(7); + + indices.add(5); + indices.add(1); + indices.add(7); + + indices.add(1); + indices.add(3); + indices.add(7); + + indices.add(4); + indices.add(5); + indices.add(6); + + indices.add(5); + indices.add(7); + indices.add(6); + + indices.add(0); + indices.add(4); + indices.add(2); + + indices.add(2); + indices.add(4); + indices.add(6); + + indices.add(0); + indices.add(1); + indices.add(4); + + indices.add(5); + indices.add(4); + indices.add(1); + return Mesh.create(vertices, indices); + } + +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/shape/Cone.java b/org.simantics.g3d/src/org/simantics/g3d/shape/Cone.java index 2f378405..5283eb44 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/shape/Cone.java +++ b/org.simantics.g3d/src/org/simantics/g3d/shape/Cone.java @@ -1,75 +1,75 @@ -/******************************************************************************* - * Copyright (c) 2012, 2013 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.g3d.shape; - -import java.util.ArrayList; -import java.util.List; - -import javax.vecmath.AxisAngle4d; -import javax.vecmath.Vector3d; - -import org.simantics.g3d.math.MathTools; - -public class Cone { - - public static Mesh create(double radius, int s) { - if (s < 3 || radius < MathTools.NEAR_ZERO) - throw new IllegalArgumentException(); - List vertices = new ArrayList(s+2); - List normals = new ArrayList(vertices.size()); - List indices = new ArrayList(); - - vertices.add(new Vector3d(0.0,0.0,0.0)); - normals.add(new Vector3d(0.0,-1.0,0.0)); - vertices.add(new Vector3d(0.0,radius*2.0,0.0)); - normals.add(new Vector3d(0.0,1.0,0.0)); - - Vector3d v = new Vector3d(radius,0,0); - for (int i = 0; i < s; i++) { - AxisAngle4d aa = new AxisAngle4d(0,1,0,((double)i/(double)s)*Math.PI * 2); - Vector3d t = new Vector3d(); - MathTools.rotate(MathTools.getQuat(aa), v, t); - vertices.add(t); - Vector3d n = new Vector3d(t); - n.normalize(); - normals.add(n); - } - - for (int i = 0; i < s; i++) { - indices.add(0); - - if (i < s - 1) - indices.add(i + 3); - else - indices.add(2); - indices.add(i + 2); - } - - for (int i = 0; i < s; i++) { - indices.add(1); - indices.add(i + 2); - if (i < s - 1) - indices.add(i + 3); - else - indices.add(2); - - } - return new Mesh(vertices,normals,indices); - - } - - public static void main(String arg[]) { - Mesh s1 = create(1.0, 3); - Mesh s2 = create(1.0, 4); - System.out.println("debug " + s1 + "\n" + s2); - } -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.shape; + +import java.util.ArrayList; +import java.util.List; + +import javax.vecmath.AxisAngle4d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.math.MathTools; + +public class Cone { + + public static Mesh create(double radius, int s) { + if (s < 3 || radius < MathTools.NEAR_ZERO) + throw new IllegalArgumentException(); + List vertices = new ArrayList(s+2); + List normals = new ArrayList(vertices.size()); + List indices = new ArrayList(); + + vertices.add(new Vector3d(0.0,0.0,0.0)); + normals.add(new Vector3d(0.0,-1.0,0.0)); + vertices.add(new Vector3d(0.0,radius*2.0,0.0)); + normals.add(new Vector3d(0.0,1.0,0.0)); + + Vector3d v = new Vector3d(radius,0,0); + for (int i = 0; i < s; i++) { + AxisAngle4d aa = new AxisAngle4d(0,1,0,((double)i/(double)s)*Math.PI * 2); + Vector3d t = new Vector3d(); + MathTools.rotate(MathTools.getQuat(aa), v, t); + vertices.add(t); + Vector3d n = new Vector3d(t); + n.normalize(); + normals.add(n); + } + + for (int i = 0; i < s; i++) { + indices.add(0); + + if (i < s - 1) + indices.add(i + 3); + else + indices.add(2); + indices.add(i + 2); + } + + for (int i = 0; i < s; i++) { + indices.add(1); + indices.add(i + 2); + if (i < s - 1) + indices.add(i + 3); + else + indices.add(2); + + } + return Mesh.create(vertices,normals,indices); + + } + + public static void main(String arg[]) { + Mesh s1 = create(1.0, 3); + Mesh s2 = create(1.0, 4); + System.out.println("debug " + s1 + "\n" + s2); + } +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/shape/Cylinder.java b/org.simantics.g3d/src/org/simantics/g3d/shape/Cylinder.java index 5398512e..4fe564bf 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/shape/Cylinder.java +++ b/org.simantics.g3d/src/org/simantics/g3d/shape/Cylinder.java @@ -1,34 +1,35 @@ -/******************************************************************************* - * Copyright (c) 2012, 2013 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.g3d.shape; - -import java.util.ArrayList; -import java.util.List; - -import javax.vecmath.Vector3d; - -public class Cylinder { - - public static Mesh create(Vector3d start, Vector3d dir, double r, int s) { - Tube tube = new Tube(); - tube.setResolution(s); - tube.setRadius(r); - List vertices = new ArrayList(); - vertices.add(start); - Vector3d t = new Vector3d(start); - t.add(dir); - vertices.add(dir); - tube.setVertices(vertices); - return tube.create(); - } - -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.shape; + +import java.util.ArrayList; +import java.util.List; + +import javax.vecmath.Tuple3d; +import javax.vecmath.Vector3d; + +public class Cylinder { + + public static Mesh create(Vector3d start, Vector3d dir, double r, int s) { + Tube tube = new Tube(); + tube.setResolution(s); + tube.setRadius(r); + List vertices = new ArrayList(); + vertices.add(start); + Vector3d t = new Vector3d(start); + t.add(dir); + vertices.add(dir); + tube.setVertices(vertices); + return tube.create(); + } + +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java b/org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java index ea6ec593..a45cf812 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java +++ b/org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java @@ -1,148 +1,164 @@ -/******************************************************************************* - * Copyright (c) 2012, 2013 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.g3d.shape; - -import java.util.ArrayList; -import java.util.List; - -import javax.vecmath.Quat4d; -import javax.vecmath.Vector3d; - -import org.simantics.g3d.math.MathTools; - -public class Mesh { - private List vertices; - private List normals; - private List colors; - private List indices; - - - public Mesh(List vertices, List normals, - List colors, List indices) { - this.vertices = vertices; - this.normals = normals; - this.colors = colors; - this.indices = indices; - } - - public Mesh(List vertices, List normals, List indices) { - this.vertices = vertices; - this.normals = normals; - this.indices = indices; - } - - public Mesh(List vertices, List indices) { - this.vertices = vertices; - this.indices = indices; - } - - public List getVertices() { - return vertices; - } - - public List getNormals() { - return normals; - } - - public List getIndices() { - return indices; - } - - public List getColors() { - return colors; - } - - public void createNormals() { - normals = new ArrayList(vertices.size()); - for (int i = 0; i < vertices.size(); i++) { - normals.add(new Vector3d()); - } - Vector3d v1 = new Vector3d(); - Vector3d v2 = new Vector3d(); - Vector3d v3 = new Vector3d(); - Vector3d t1 = new Vector3d(); - Vector3d t2 = new Vector3d(); - Vector3d n = new Vector3d(); - for (int i = 0; i < indices.size(); i+=3) { - v1.set(vertices.get(i)); - v2.set(vertices.get(i+1)); - v3.set(vertices.get(i+2)); - t1.sub(v3,v1); - t2.sub(v2,v1); - n.cross(t2, t1); - normals.get(i).add(n); - normals.get(i+1).add(n); - normals.get(i+2).add(n); - } - for (int i = 0; i < normals.size(); i++) { - normals.get(i).normalize(); - } - } - - public void translate(Vector3d v) { - for (int i = 0; i < vertices.size(); i++) { - vertices.get(i).add(v); - } - } - - public void rotate(Quat4d q) { - Vector3d t = new Vector3d(); - for (int i = 0; i < vertices.size(); i++) { - MathTools.rotate(q, vertices.get(i), t); - vertices.get(i).set(t); - } - - if (normals != null) { - for (int i = 0; i < normals.size(); i++) { - MathTools.rotate(q, normals.get(i), t); - t.normalize(); - normals.get(i).set(t); - } - } - } - - public void setColor(Color4d color) { - colors = new ArrayList(vertices.size()); - for (int i = 0; i < vertices.size(); i++) { - colors.add(color); - } - } - - public void add(Mesh mesh) { - int vindex = vertices.size(); - int triIndex = indices.size(); - vertices.addAll(mesh.getVertices()); - indices.addAll(mesh.indices); - for (int i = triIndex; i < indices.size(); i++) { - indices.set(i, indices.get(i)+vindex); - } - if (normals != null) { - boolean hasNormals = true; - if (mesh.getNormals() == null) { - mesh.createNormals(); - hasNormals = false; - } - normals.addAll(mesh.getNormals()); - if (!hasNormals) - mesh.normals = null; - } - if (colors != null) { - if (mesh.getColors() != null) { - colors.addAll(mesh.getColors()); - } else { - for (int i = 0; i < mesh.getVertices().size(); i++) { - colors.add(new Color4d(1,1,1,0)); - } - } - } - } -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.shape; + +import java.util.ArrayList; +import java.util.List; + +import javax.vecmath.Quat4d; +import javax.vecmath.Tuple3d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.math.MathTools; + +public class Mesh { + private List vertices; + private List normals; + private List colors; + private List indices; + + + public Mesh(List vertices, List normals, + List colors, List indices) { + this.vertices = vertices; + this.normals = normals; + this.colors = colors; + this.indices = indices; + } + + public Mesh(List vertices, List normals, List indices) { + this.vertices = vertices; + this.normals = normals; + this.indices = indices; + } + + public static Mesh create(List vertices, List normals, List indices) { + + List v = new ArrayList(); + List n = new ArrayList(); + v.addAll(vertices); + n.addAll(normals); + return new Mesh(v, n, indices); + } + + public Mesh(List vertices, List indices) { + this.vertices = vertices; + this.indices = indices; + } + + public static Mesh create(List vertices, List indices) { + List v = new ArrayList(); + v.addAll(vertices); + return new Mesh(v, indices); + } + + public List getVertices() { + return vertices; + } + + public List getNormals() { + return normals; + } + + public List getIndices() { + return indices; + } + + public List getColors() { + return colors; + } + + public void createNormals() { + normals = new ArrayList(vertices.size()); + for (int i = 0; i < vertices.size(); i++) { + normals.add(new Vector3d()); + } + Vector3d v1 = new Vector3d(); + Vector3d v2 = new Vector3d(); + Vector3d v3 = new Vector3d(); + Vector3d t1 = new Vector3d(); + Vector3d t2 = new Vector3d(); + Vector3d n = new Vector3d(); + for (int i = 0; i < indices.size(); i+=3) { + v1.set(vertices.get(i)); + v2.set(vertices.get(i+1)); + v3.set(vertices.get(i+2)); + t1.sub(v3,v1); + t2.sub(v2,v1); + n.cross(t2, t1); + normals.get(i).add(n); + normals.get(i+1).add(n); + normals.get(i+2).add(n); + } + for (int i = 0; i < normals.size(); i++) { + ((Vector3d)normals.get(i)).normalize(); + } + } + + public void translate(Vector3d v) { + for (int i = 0; i < vertices.size(); i++) { + vertices.get(i).add(v); + } + } + + public void rotate(Quat4d q) { + Vector3d t = new Vector3d(); + for (int i = 0; i < vertices.size(); i++) { + MathTools.rotate(q, vertices.get(i), t); + vertices.get(i).set(t); + } + + if (normals != null) { + for (int i = 0; i < normals.size(); i++) { + MathTools.rotate(q, normals.get(i), t); + t.normalize(); + normals.get(i).set(t); + } + } + } + + public void setColor(Color4d color) { + colors = new ArrayList(vertices.size()); + for (int i = 0; i < vertices.size(); i++) { + colors.add(color); + } + } + + public void add(Mesh mesh) { + int vindex = vertices.size(); + int triIndex = indices.size(); + vertices.addAll(mesh.getVertices()); + indices.addAll(mesh.indices); + for (int i = triIndex; i < indices.size(); i++) { + indices.set(i, indices.get(i)+vindex); + } + if (normals != null) { + boolean hasNormals = true; + if (mesh.getNormals() == null) { + mesh.createNormals(); + hasNormals = false; + } + normals.addAll(mesh.getNormals()); + if (!hasNormals) + mesh.normals = null; + } + if (colors != null) { + if (mesh.getColors() != null) { + colors.addAll(mesh.getColors()); + } else { + for (int i = 0; i < mesh.getVertices().size(); i++) { + colors.add(new Color4d(1,1,1,0)); + } + } + } + } +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/shape/Sphere.java b/org.simantics.g3d/src/org/simantics/g3d/shape/Sphere.java index 6eb26dfa..d007175a 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/shape/Sphere.java +++ b/org.simantics.g3d/src/org/simantics/g3d/shape/Sphere.java @@ -1,105 +1,105 @@ -/******************************************************************************* - * Copyright (c) 2012, 2013 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.g3d.shape; - -import java.util.ArrayList; -import java.util.List; - -import javax.vecmath.AxisAngle4d; -import javax.vecmath.Vector3d; - -import org.simantics.g3d.math.MathTools; - -public class Sphere { - - public static Mesh create(double radius, int s, int p) { - if (s < 3 || p < 3 || radius < MathTools.NEAR_ZERO) - throw new IllegalArgumentException(); - List vertices = new ArrayList((s-2)*p + 2); - List normals = new ArrayList(vertices.size()); - List indices = new ArrayList(((s-3)*p*2 + p * 2)*3); - Vector3d v = new Vector3d(0.0,-radius,0.0); - Vector3d vp = new Vector3d(); - for (int ip = 0; ip < p; ip++) { - if (ip == 0) { - vertices.add(new Vector3d(0.0,-radius,0.0)); - } else if (ip == p - 1) { - vertices.add(new Vector3d(0.0, radius,0.0)); - int off = 1 + (ip-2)*s; - for (int is = 0; is < s; is++) { - indices.add(vertices.size() - 1); - indices.add(is+off); - if (is < s -1) - indices.add(is+off+1); - else - indices.add(off); - - - } - } else { - AxisAngle4d aa = new AxisAngle4d(1, 0, 0, ((double)ip/(double)(p-1))*Math.PI); - MathTools.rotate(MathTools.getQuat(aa), v, vp); - for (int is = 0; is < s; is++) { - aa = new AxisAngle4d(0, 1, 0, ((double)is/(double)s)*Math.PI*2); - Vector3d vs = new Vector3d(); - MathTools.rotate(MathTools.getQuat(aa), vp, vs); - vertices.add(vs); - } - if (ip == 1) { - for (int is = 0; is < s; is++) { - indices.add(0); - if (is < s -1) - indices.add(is+2); - else - indices.add(1); - indices.add(is+1); - } - } else { - int off = 1 + (ip-1)*s; - for (int is = 0; is < s-1; is++) { - indices.add(off + is - s); - indices.add(off + is+1); - indices.add(off + is); - - - indices.add(off + is - s); - indices.add(off + is + 1 - s); - indices.add(off + is + 1); - - } - indices.add(off - 1); - indices.add(off); - indices.add(off + s - 1); - - indices.add(off -1); - indices.add(off - s); - indices.add(off); - - } - } - } - for (int i = 0; i < vertices.size(); i++) { - Vector3d n = new Vector3d(vertices.get(i)); - n.normalize(); - normals.add(n); - } - - return new Mesh(vertices,normals,indices); - - } - - public static void main(String arg[]) { - Mesh s1 = create(1.0, 3, 3); - Mesh s2 = create(1.0, 4, 4); - System.out.println("debug " + s1 + " " + s2); - } -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.shape; + +import java.util.ArrayList; +import java.util.List; + +import javax.vecmath.AxisAngle4d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.math.MathTools; + +public class Sphere { + + public static Mesh create(double radius, int s, int p) { + if (s < 3 || p < 3 || radius < MathTools.NEAR_ZERO) + throw new IllegalArgumentException(); + List vertices = new ArrayList((s-2)*p + 2); + List normals = new ArrayList(vertices.size()); + List indices = new ArrayList(((s-3)*p*2 + p * 2)*3); + Vector3d v = new Vector3d(0.0,-radius,0.0); + Vector3d vp = new Vector3d(); + for (int ip = 0; ip < p; ip++) { + if (ip == 0) { + vertices.add(new Vector3d(0.0,-radius,0.0)); + } else if (ip == p - 1) { + vertices.add(new Vector3d(0.0, radius,0.0)); + int off = 1 + (ip-2)*s; + for (int is = 0; is < s; is++) { + indices.add(vertices.size() - 1); + indices.add(is+off); + if (is < s -1) + indices.add(is+off+1); + else + indices.add(off); + + + } + } else { + AxisAngle4d aa = new AxisAngle4d(1, 0, 0, ((double)ip/(double)(p-1))*Math.PI); + MathTools.rotate(MathTools.getQuat(aa), v, vp); + for (int is = 0; is < s; is++) { + aa = new AxisAngle4d(0, 1, 0, ((double)is/(double)s)*Math.PI*2); + Vector3d vs = new Vector3d(); + MathTools.rotate(MathTools.getQuat(aa), vp, vs); + vertices.add(vs); + } + if (ip == 1) { + for (int is = 0; is < s; is++) { + indices.add(0); + if (is < s -1) + indices.add(is+2); + else + indices.add(1); + indices.add(is+1); + } + } else { + int off = 1 + (ip-1)*s; + for (int is = 0; is < s-1; is++) { + indices.add(off + is - s); + indices.add(off + is+1); + indices.add(off + is); + + + indices.add(off + is - s); + indices.add(off + is + 1 - s); + indices.add(off + is + 1); + + } + indices.add(off - 1); + indices.add(off); + indices.add(off + s - 1); + + indices.add(off -1); + indices.add(off - s); + indices.add(off); + + } + } + } + for (int i = 0; i < vertices.size(); i++) { + Vector3d n = new Vector3d(vertices.get(i)); + n.normalize(); + normals.add(n); + } + + return Mesh.create(vertices,normals,indices); + + } + + public static void main(String arg[]) { + Mesh s1 = create(1.0, 3, 3); + Mesh s2 = create(1.0, 4, 4); + System.out.println("debug " + s1 + " " + s2); + } +} diff --git a/org.simantics.g3d/src/org/simantics/g3d/shape/Tube.java b/org.simantics.g3d/src/org/simantics/g3d/shape/Tube.java index e4d89bb2..828fd269 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/shape/Tube.java +++ b/org.simantics.g3d/src/org/simantics/g3d/shape/Tube.java @@ -1,184 +1,192 @@ -/******************************************************************************* - * Copyright (c) 2012, 2013 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.g3d.shape; - -import java.util.ArrayList; -import java.util.List; - -import javax.vecmath.AxisAngle4d; -import javax.vecmath.Vector3d; - -import org.simantics.g3d.math.MathTools; - - -public class Tube { - List vertices; - List colors; - List radiis; - Double radius = 1.0; - int resolution = 8; - - - public void setResolution(int resolution) { - if (resolution > 2) - this.resolution = resolution; - } - - public void setVertices(List vertices) { - this.vertices = vertices; - } - - public void setColors(List colors) { - this.colors = colors; - } - - public void setRadiis(List radiis) { - this.radiis = radiis; - } - - public void setRadius(Double radius) { - this.radius = radius; - } - - - - public Mesh create() { - if (vertices.size() < 2 ) - throw new IllegalArgumentException("Tube must have at least two vertices"); - - Vector3d t = new Vector3d(); - - for (int i = 0; i < vertices.size() - 1; i++) { - t.set(vertices.get(i+1)); - t.sub(vertices.get(i)); - if (t.lengthSquared() < 0.0001) - throw new IllegalArgumentException("vertices at index " + i + " are too close to each other"); - } - - List points = new ArrayList(vertices.size()*resolution); - List normals = new ArrayList(vertices.size()*resolution); - - for (int i = 0; i < vertices.size(); i++) { - createCircle(i,points,normals); - } - - int index[] = new int[(vertices.size()-1)*resolution*6]; - - createIndices(index); - List indices = new ArrayList(); - for (int i = 0; i < index.length; i++) { - indices.add(index[i]); - } - - - - vertices.clear(); - if (colors != null) - colors.clear(); - if (radiis != null) - radiis.clear(); - - return new Mesh(points, normals, indices); - - } - - private void createCircle(int i, List points, List normals) { - final Vector3d up = new Vector3d(0,1,0); - final Vector3d up2 = new Vector3d(0,0,1); - Vector3d p = vertices.get(i); - Vector3d t = getTangent(i); - Vector3d n = new Vector3d(); - if (up.dot(t) < 0.99) { - n.cross(up, t); - } else { - n.cross(up2, t); - } - n.normalize(); - if (radiis != null) { - n.scale(radiis.get(i)); - } else { - n.scale(radius); - } - - for (int index = 0; index < resolution; index ++) { - Vector3d v; - if (index == 0) { - v = new Vector3d(n); - - } else { - AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)resolution); - v = new Vector3d(); - MathTools.rotate(MathTools.getQuat(aa), n, v); - } - //int vIndex = (i*resolution + index)*3; - Vector3d pt = new Vector3d(p); - pt.add(v); - //points.set(vIndex, pt); - points.add(pt); - v.normalize(); - //normals.set(vIndex, v); - normals.add(v); - } - } - - private Vector3d getTangent(int i) { - Vector3d p,n; - if (i == 0) { - p = vertices.get(0); - n = vertices.get(1); - } else if (i == vertices.size() - 1) { - p = vertices.get(i-1); - n = vertices.get(i); - } else { - p = vertices.get(i-1); - n = vertices.get(i+1); - } - n = new Vector3d(n); - n.sub(p); - n.normalize(); - return n; - } - - private void createIndices(int index[]) { - for (int c = 0; c < vertices.size() - 1; c++) { - for (int s = 0; s < resolution; s++) { - int ii = (c * resolution + s) * 6; - int iv = c*resolution + s; - - /* - iv+1 ---- iv + resolution + 1 - | /| - |/ | - iv ---- iv + resolution - */ - if (s < resolution - 1) { - index[ii+2] = iv; - index[ii+1] = iv+resolution; - index[ii+0] = iv+resolution+1; - - index[ii+5] = iv; - index[ii+4] = iv+resolution+1; - index[ii+3] = iv+1; - } else { - index[ii+2] = iv; - index[ii+1] = iv+resolution; - index[ii+0] = iv+1; - - index[ii+5] = iv; - index[ii+4] = iv+1; - index[ii+3] = iv+1-resolution; - } - } - } - } - -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.shape; + +import java.util.ArrayList; +import java.util.List; + +import javax.vecmath.AxisAngle4d; +import javax.vecmath.Tuple3d; +import javax.vecmath.Vector3d; + +import org.simantics.g3d.math.MathTools; + + +public class Tube { + List vertices; + List tangents; + List colors; + List radiis; + Double radius = 1.0; + int resolution = 8; + + + public void setResolution(int resolution) { + if (resolution > 2) + this.resolution = resolution; + } + + public void setVertices(List vertices) { + this.vertices = vertices; + } + + public void setTangents(List tangents) { + this.tangents = tangents; + } + + public void setColors(List colors) { + this.colors = colors; + } + + public void setRadiis(List radiis) { + this.radiis = radiis; + } + + public void setRadius(Double radius) { + this.radius = radius; + } + + + + public Mesh create() { + if (vertices.size() < 2 ) + throw new IllegalArgumentException("Tube must have at least two vertices"); + + Vector3d t = new Vector3d(); + + for (int i = 0; i < vertices.size() - 1; i++) { + t.set(vertices.get(i+1)); + t.sub(vertices.get(i)); + if (t.lengthSquared() < 0.0001) + throw new IllegalArgumentException("vertices at index " + i + " are too close to each other"); + } + + List points = new ArrayList(vertices.size()*resolution); + List normals = new ArrayList(vertices.size()*resolution); + + for (int i = 0; i < vertices.size(); i++) { + createCircle(i,points,normals); + } + + int index[] = new int[(vertices.size()-1)*resolution*6]; + + createIndices(index); + List indices = new ArrayList(); + for (int i = 0; i < index.length; i++) { + indices.add(index[i]); + } + + + + vertices.clear(); + if (colors != null) + colors.clear(); + if (radiis != null) + radiis.clear(); + + return Mesh.create(points, normals, indices); + + } + + private void createCircle(int i, List points, List normals) { + final Vector3d up = new Vector3d(0,1,0); + final Vector3d up2 = new Vector3d(0,0,1); + Tuple3d p = vertices.get(i); + Vector3d t = getTangent(i); + Vector3d n = new Vector3d(); + if (Math.abs(up.dot(t)) < 0.99) { + n.cross(up, t); + } else { + n.cross(up2, t); + } + n.normalize(); + if (radiis != null) { + n.scale(radiis.get(i)); + } else { + n.scale(radius); + } + + for (int index = 0; index < resolution; index ++) { + Vector3d v; + if (index == 0) { + v = new Vector3d(n); + + } else { + AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)resolution); + v = new Vector3d(); + MathTools.rotate(MathTools.getQuat(aa), n, v); + } + //int vIndex = (i*resolution + index)*3; + Vector3d pt = new Vector3d(p); + pt.add(v); + //points.set(vIndex, pt); + points.add(pt); + v.normalize(); + //normals.set(vIndex, v); + normals.add(v); + } + } + + private Vector3d getTangent(int i) { + Tuple3d p,n; + if (tangents != null) + return tangents.get(i); + if (i == 0) { + p = vertices.get(0); + n = vertices.get(1); + } else if (i == vertices.size() - 1) { + p = vertices.get(i-1); + n = vertices.get(i); + } else { + p = vertices.get(i-1); + n = vertices.get(i+1); + } + Vector3d nn = new Vector3d(n); + nn.sub(p); + nn.normalize(); + return nn; + } + + private void createIndices(int index[]) { + for (int c = 0; c < vertices.size() - 1; c++) { + for (int s = 0; s < resolution; s++) { + int ii = (c * resolution + s) * 6; + int iv = c*resolution + s; + + /* + iv+1 ---- iv + resolution + 1 + | /| + |/ | + iv ---- iv + resolution + */ + if (s < resolution - 1) { + index[ii+2] = iv; + index[ii+1] = iv+resolution; + index[ii+0] = iv+resolution+1; + + index[ii+5] = iv; + index[ii+4] = iv+resolution+1; + index[ii+3] = iv+1; + } else { + index[ii+2] = iv; + index[ii+1] = iv+resolution; + index[ii+0] = iv+1; + + index[ii+5] = iv; + index[ii+4] = iv+1; + index[ii+3] = iv+1-resolution; + } + } + } + } + +} -- 2.47.1