001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wpvs/j3d/DefaultSurface.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2008 by:
006 EXSE, Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstr. 19
030 53177 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042 ---------------------------------------------------------------------------*/
043 package org.deegree.ogcwebservices.wpvs.j3d;
044
045 import javax.media.j3d.Geometry;
046 import javax.media.j3d.Shape3D;
047
048 import org.deegree.model.spatialschema.Position;
049 import org.deegree.model.spatialschema.Ring;
050 import org.deegree.model.spatialschema.Surface;
051
052 import com.sun.j3d.utils.geometry.GeometryInfo;
053 import com.sun.j3d.utils.geometry.NormalGenerator;
054
055 /**
056 *
057 *
058 *
059 * @version $Revision: 9345 $
060 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
061 * @author last edited by: $Author: apoth $
062 *
063 * @version 1.0. $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
064 *
065 * @since 2.0
066 */
067 public class DefaultSurface extends Shape3D {
068
069 protected Surface surface;
070
071 private String parentID;
072
073 private String objectID;
074
075 /**
076 *
077 * @param objectID
078 * @param parentID
079 * @param surface
080 */
081 public DefaultSurface( String objectID, String parentID, Surface surface ) {
082 super();
083 this.surface = surface;
084 this.parentID = parentID;
085 this.objectID = objectID;
086
087 /*
088 * System.out.println( "new Appearance" ); Color3f specular = new Color3f( 0.7f, 0.7f, 0.7f );
089 * Color3f white = new Color3f( 0.4f, 1, 0.4f );
090 *
091 * Material targetMaterial = new Material(); targetMaterial.setAmbientColor( white );
092 * targetMaterial.setDiffuseColor( white ); targetMaterial.setSpecularColor( specular );
093 * targetMaterial.setShininess( 75.0f ); targetMaterial.setLightingEnable( true );
094 * targetMaterial.setCapability( Material.ALLOW_COMPONENT_WRITE ); // ColoringAttributes ca =
095 * new ColoringAttributes(); ca.setShadeModel( ColoringAttributes.SHADE_GOURAUD ); //
096 * Appearance defaultAppearance = new Appearance(); defaultAppearance.setMaterial(
097 * targetMaterial ); defaultAppearance.setColoringAttributes( ca ); // PolygonAttributes
098 * targetPolyAttr = new PolygonAttributes(); targetPolyAttr.setCapability(
099 * PolygonAttributes.ALLOW_MODE_WRITE ); targetPolyAttr.setCapability(
100 * PolygonAttributes.ALLOW_NORMAL_FLIP_WRITE ); targetPolyAttr.setPolygonMode(
101 * PolygonAttributes.POLYGON_FILL ); targetPolyAttr.setCullFace( PolygonAttributes.CULL_NONE ); //
102 * pa.setPolygonMode( PolygonAttributes.POLYGON_LINE );
103 * defaultAppearance.setPolygonAttributes( targetPolyAttr ); setAppearance(
104 * defaultAppearance );
105 */
106 }
107
108 /**
109 * @return the ID of the Object this Surface is a part of (e.g. the building id if this is a
110 * wall)
111 */
112 public String getParentID() {
113 return parentID;
114 }
115
116 /**
117 * @return the objectID value.
118 */
119 public String getObjectID() {
120 return objectID;
121 }
122
123 /**
124 * @return a String composited of the parentID and "_" and the objectID
125 */
126 public String getDefaultSurfaceID() {
127 return parentID + '_' + objectID;
128 }
129
130 /**
131 *
132 * @return the surface geometry encapsulated
133 */
134 public Surface getSurfaceGeometry() {
135 return surface;
136 }
137
138 /**
139 * this method must be called before adding the surface to a Group
140 */
141 public void compile() {
142
143 GeometryInfo geometryInfo = new GeometryInfo( GeometryInfo.POLYGON_ARRAY );
144
145 Position[] pos = surface.getSurfaceBoundary().getExteriorRing().getPositions();
146 Ring[] innerRings = surface.getSurfaceBoundary().getInteriorRings();
147 int k = 1;
148 int l = 3 * ( pos.length );
149 if ( innerRings != null ) {
150 for ( int i = 0; i < innerRings.length; i++ ) {
151 k++;
152 l += ( 3 * innerRings[i].getPositions().length );
153 }
154 }
155
156 float[] coords = new float[l];
157 int contourCounts[] = { k };
158 int[] stripCounts = new int[k];
159 k = 0;
160 stripCounts[k++] = pos.length;
161
162 int z = 0;
163 for ( int i = 0; i < pos.length; i++ ) {
164 coords[z++] = (float) pos[i].getX();
165 coords[z++] = (float) pos[i].getY();
166 coords[z++] = (float) pos[i].getZ();
167 }
168
169 if ( innerRings != null ) {
170 for ( int j = 0; j < innerRings.length; j++ ) {
171 pos = innerRings[j].getPositions();
172 stripCounts[k++] = pos.length;
173 for ( int i = 0; i < pos.length; i++ ) {
174 coords[z++] = (float) pos[i].getX();
175 coords[z++] = (float) pos[i].getY();
176 coords[z++] = (float) pos[i].getZ();
177 }
178 }
179 }
180
181 geometryInfo.setCoordinates( coords );
182 geometryInfo.setStripCounts( stripCounts );
183 geometryInfo.setContourCounts( contourCounts );
184
185 NormalGenerator ng = new NormalGenerator();
186 ng.generateNormals( geometryInfo );
187
188 setGeometry( geometryInfo.getGeometryArray() );
189
190 setAppearanceOverrideEnable( true );
191 // setAppearance( getAppearance() );
192 }
193
194 /**
195 * @return a String representation of all the geometries inside this surface
196 */
197 public String getGeometryAsString() {
198 StringBuffer sb = new StringBuffer( numGeometries() );
199 for ( int i = 0; i < numGeometries(); ++i ) {
200 Geometry ga = getGeometry( i );
201 sb.append( ga.toString() );
202 }
203 return sb.toString();
204 }
205 }