MetricSpacePolygon.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.android.view.visualization.shape;
00018 
00019 import com.google.common.base.Preconditions;
00020 import com.google.common.collect.Lists;
00021 
00022 import org.ros.android.view.visualization.Color;
00023 import org.ros.android.view.visualization.Vertices;
00024 import org.ros.android.view.visualization.VisualizationView;
00025 
00026 import java.nio.FloatBuffer;
00027 import java.util.List;
00028 
00029 import javax.microedition.khronos.opengles.GL10;
00030 
00036 public class MetricSpacePolygon extends BaseShape {
00037 
00038   final FloatBuffer vertexBuffer;
00039   final List<FloatBuffer> triangles;
00040 
00041   public MetricSpacePolygon(final float[] vertices, final Color color) {
00042     super();
00043     vertexBuffer = Vertices.toFloatBuffer(vertices);
00044     setColor(color);
00045 
00046     final List<Triangulate.Point> points = Lists.newArrayList();
00047     final Triangulate.Point[] contour = new Triangulate.Point[vertices.length / 3];
00048     for (int i = 0; i < contour.length; ++i) {
00049       contour[i] = new Triangulate.Point(vertices[i * 3], vertices[i * 3 + 1]);
00050     }
00051     Preconditions.checkState(Triangulate.process(contour, points));
00052 
00053     triangles = Lists.newArrayList();
00054     for (int i = 0; i < points.size() / 3; ++i) {
00055       final FloatBuffer triangle = Vertices.allocateBuffer(3 * 3);
00056       for (int j = i * 3; j < i * 3 + 3; ++j) {
00057         triangle.put(points.get(j).x());
00058         triangle.put(points.get(j).y());
00059         triangle.put(0.f);
00060       }
00061       triangle.flip();
00062       triangles.add(triangle);
00063     }
00064   }
00065 
00066   @Override
00067   public void drawShape(VisualizationView view, GL10 gl) {
00068     final Color translucent = getColor();
00069     translucent.setAlpha(0.3f);
00070     for (final FloatBuffer triangle : triangles) {
00071       Vertices.drawTriangleFan(gl, triangle, translucent);
00072     }
00073     final Color opaque = getColor();
00074     opaque.setAlpha(1.f);
00075     Vertices.drawLineLoop(gl, vertexBuffer, opaque, 3.f);
00076     Vertices.drawPoints(gl, vertexBuffer, opaque, 10.f);
00077   }
00078 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07