/******************************************************************************* * 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); } }