[COMMITTED] Circle using Line Segments

There seems to be no geometry representing Circle



Index: src/com/jme/scene/Circle.java
===================================================================
--- src/com/jme/scene/Circle.java   (revision 0)
+++ src/com/jme/scene/Circle.java   (revision 0)
@@ -0,0 +1,70 @@
+package com.jme.scene;
+
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+
+import com.jme.math.FastMath;
+import com.jme.util.geom.BufferUtils;
+
+/**
+ * As Circle consists of Line Segments, you can use Line methods to set property such as width, stipple.
+ *
+ * @author mulova
+ */
+public class Circle extends Line {
+   
+   public Circle(String name, int samples, float radius){
+      super(name);
+      FloatBuffer vertexBuf = createVertex(samples, radius);
+      FloatBuffer normalBuf = createNormal(samples);
+      FloatBuffer colorBuf = createColor(samples);
+      IntBuffer indexBuf = createIndex(samples);
+      
+      reconstruct(vertexBuf, normalBuf, colorBuf, null);
+      setIndexBuffer(indexBuf);
+      setAntialiased(true);
+      setMode(Mode.Loop);
+      setLightCombineMode(LightCombineMode.Off);
+   }
+   
+
+   private IntBuffer createIndex(int samples) {
+      IntBuffer buf = BufferUtils.createIntBuffer(samples);
+      for (int i = 0; i < samples; i++) {
+         buf.put(i);
+      }
+      return buf;
+   }
+
+
+   private FloatBuffer createColor(int samples) {
+      FloatBuffer buf = BufferUtils.createColorBuffer(samples);
+      for (int i = 0; i < samples; i++) {
+         buf.put(1).put(1).put(1).put(1);
+      }
+      return buf;
+   }
+
+
+   private FloatBuffer createNormal(int samples) {
+      FloatBuffer buf = BufferUtils.createVector3Buffer(samples);
+      for (int i = 0; i < samples; i++) {
+         buf.put(0).put(0).put(1);
+      }
+      return buf;
+   }
+   
+
+   private FloatBuffer createVertex(int sample, float radius){
+      FloatBuffer buf = BufferUtils.createVector3Buffer(sample);
+      
+      buf.rewind();
+      for(int i=0; i< sample; i++){
+         float theta = FastMath.TWO_PI / sample * i;
+         float x = FastMath.cos(theta) * radius;
+         float y = FastMath.sin(theta) * radius;
+         buf.put(x).put(y).put(0);
+      }
+      return buf;
+   }
+}



Test Code


package jmetest.scene;

import com.jme.app.SimpleGame;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Circle;

public class TestCircle extends SimpleGame {

    @Override
    protected void simpleInitGame() {
        Circle circle = new Circle("circle", 32, 128);
        circle.setLocalTranslation(128, 128, 0);
        circle.setLineWidth(1);
        circle.setSolidColor(ColorRGBA.red);
        circle.setStipplePattern((short)0xFF00);
        circle.setRenderQueueMode(Renderer.QUEUE_ORTHO);
        rootNode.attachChild(circle);
    }
   
    public static void main(String[] args) {
        TestCircle app = new TestCircle();
        app.setConfigShowMode(ConfigShowMode.AlwaysShow);
        app.start();
    }

}

basixs said:

+   /**
+    * normal로 원을 표시한다.
+    * @param name
+    * @param samples
+    * @param radius
+    * @param length
+    */


I saw that but you were too quick :P

Looks good here mulova :).

sorry.  ://
If accepted, I'll add more comments.

lol, I was just teasing you :slight_smile:



If you get the comments in then great, but it seems pretty straightforward here (so I wouldn't sweat it too much).  Extending line was a smart move ;).  The one thing I might suggest is to not have it be anti-aliased by default…

+   /**
+    * normal로 원을 표시한다.
+    * @param name
+    * @param samples
+    * @param radius
+    * @param length
+    */


I saw that but you were too quick :P

Looks good here mulova :).
basixs said:

lol, I was just teasing you :)

If you get the comments in then great, but it seems pretty straightforward here (so I wouldn't sweat it too much).  Extending line was a smart move ;).  The one thing I might suggest is to not have it be anti-aliased by default...

No, It's my pleasure. :D
Anti-aliasing is removed.
Comments are added.
If there is something incorrect, please let me know. (I'm poor at english  :'()


Index: src/com/jme/scene/Circle.java
===================================================================
--- src/com/jme/scene/Circle.java   (revision 0)
+++ src/com/jme/scene/Circle.java   (revision 0)
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2003-2009 jMonkeyEngine
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package com.jme.scene;
+
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+
+import com.jme.math.FastMath;
+import com.jme.util.geom.BufferUtils;
+
+/**
+ * <code>Circle</code> consists of Line Segments. you can use Line's methods to
+ * set circle property such as width, stipple.
+ *
+ * @author YongHoon Lim (mulova)
+ */
+public class Circle extends Line {
+
+    /**
+     * create line with
+     *
+     * @param name
+     *            the name of the scene element. This is required for
+     *            identification and comparision purposes.
+     * @param samples
+     *            sampling rate to generate circle
+     * @param radius
+     *            circle size
+     */
+    public Circle(String name, int samples, float radius) {
+        super(name);
+        FloatBuffer vertexBuf = createVertex(samples, radius);
+        FloatBuffer normalBuf = createNormal(samples);
+        FloatBuffer colorBuf = createColor(samples);
+        IntBuffer indexBuf = createIndex(samples);
+
+        reconstruct(vertexBuf, normalBuf, colorBuf, null);
+        setIndexBuffer(indexBuf);
+        setMode(Mode.Loop);
+        setLightCombineMode(LightCombineMode.Off);
+    }
+
+
+    /**
+     * @param samples
+     *            sampling rate to generate circle
+     * @return the index buffer of this circle
+     */
+    private IntBuffer createIndex(int samples) {
+        IntBuffer buf = BufferUtils.createIntBuffer(samples);
+        for (int i = 0; i < samples; i++) {
+            buf.put(i);
+        }
+        return buf;
+    }
+
+
+    /**
+     * @param samples
+     *            sampling rate to generate circle
+     * @return the color buffer of this circle
+     */
+    private FloatBuffer createColor(int samples) {
+        FloatBuffer buf = BufferUtils.createColorBuffer(samples);
+        for (int i = 0; i < samples; i++) {
+            buf.put(1).put(1).put(1).put(1);
+        }
+        return buf;
+    }
+
+
+    /**
+     * @param samples
+     *            sampling rate to generate circle
+     * @return the normal buffer of this circle
+     */
+    private FloatBuffer createNormal(int samples) {
+        FloatBuffer buf = BufferUtils.createVector3Buffer(samples);
+        for (int i = 0; i < samples; i++) {
+            buf.put(0).put(0).put(1);
+        }
+        return buf;
+    }
+
+
+    /**
+     * @param samples
+     *            sampling rate to generate circle
+     * @param radius
+     *            circle size
+     * @return the vertex buffer of this circle
+     */
+    private FloatBuffer createVertex(int sample, float radius) {
+        FloatBuffer buf = BufferUtils.createVector3Buffer(sample);
+
+        buf.rewind();
+        for (int i = 0; i < sample; i++) {
+            float theta = FastMath.TWO_PI / sample * i;
+            float x = FastMath.cos(theta) * radius;
+            float y = FastMath.sin(theta) * radius;
+            buf.put(x).put(y).put(0);
+        }
+        return buf;
+    }
+}