JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
MiscUtils.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29
30package com.jogamp.opengl.test.junit.util;
31
32import java.io.BufferedReader;
33import java.io.IOException;
34import java.io.InputStream;
35import java.io.InputStreamReader;
36import java.io.OutputStream;
37import java.lang.reflect.*;
38import java.nio.FloatBuffer;
39import java.util.Iterator;
40import java.util.List;
41
42import com.jogamp.opengl.GLContext;
43import com.jogamp.common.os.Platform;
44import com.jogamp.common.util.InterruptSource;
45import com.jogamp.math.Matrix4f;
46
47public class MiscUtils {
48 public static boolean atob(final String str, final boolean def) {
49 try {
50 return Boolean.parseBoolean(str);
51 } catch (final Exception ex) {
52 ex.printStackTrace();
53 }
54 return def;
55 }
56
57 public static int atoi(final String str, final int def) {
58 try {
59 return Integer.parseInt(str);
60 } catch (final Exception ex) {
61 ex.printStackTrace();
62 }
63 return def;
64 }
65
66 public static long atol(final String str, final long def) {
67 try {
68 return Long.parseLong(str);
69 } catch (final Exception ex) {
70 ex.printStackTrace();
71 }
72 return def;
73 }
74
75 public static float atof(final String str, final float def) {
76 try {
77 return Float.parseFloat(str);
78 } catch (final Exception ex) {
79 ex.printStackTrace();
80 }
81 return def;
82 }
83
84 public static String toHexString(final byte hex) {
85 return "0x" + Integer.toHexString( hex & 0x000000FF );
86 }
87
88 public static String toHexString(final short hex) {
89 return "0x" + Integer.toHexString( hex & 0x0000FFFF );
90 }
91
92 public static String toHexString(final int hex) {
93 return "0x" + Integer.toHexString( hex );
94 }
95
96 public static String toHexString(final long hex) {
97 return "0x" + Long.toHexString( hex );
98 }
99
100 public static void waitForKey(final String preMessage) {
101 final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
102 System.err.println(preMessage+"> Press enter to continue");
103 try {
104 System.err.println(stdin.readLine());
105 } catch (final IOException e) { e.printStackTrace(); }
106 }
107
108 public static void assertFloatBufferEquals(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta) {
109 if(null == expected && null == actual) {
110 return;
111 }
112 final String msg = null != errmsg ? errmsg + " " : "";
113 if(null == expected) {
114 throw new AssertionError(msg+"; Expected is null, but actual not: "+actual);
115 }
116 if(null == actual) {
117 throw new AssertionError(msg+"; Actual is null, but expected not: "+expected);
118 }
119 if(expected.remaining() != actual.remaining()) {
120 throw new AssertionError(msg+"; Expected has "+expected.remaining()+" remaining, but actual has "+actual.remaining());
121 }
122 final int a0 = expected.position();
123 final int b0 = actual.position();
124 for(int i=0; i<expected.remaining(); i++) {
125 final float ai = expected.get(a0 + i);
126 final float bi = actual.get(b0 + i);
127 final float daibi = Math.abs(ai - bi);
128 if( daibi > delta ) {
129 throw new AssertionError(msg+"; Expected @ ["+a0+"+"+i+"] has "+ai+", but actual @ ["+b0+"+"+i+"] has "+bi+", it's delta "+daibi+" > "+delta);
130 }
131 }
132 }
133 public static void assertFloatBufferNotEqual(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta) {
134 if(null == expected || null == actual) {
135 return;
136 }
137 if(expected.remaining() != actual.remaining()) {
138 return;
139 }
140 final String msg = null != errmsg ? errmsg + " " : "";
141 final int a0 = expected.position();
142 final int b0 = actual.position();
143 for(int i=0; i<expected.remaining(); i++) {
144 final float ai = expected.get(a0 + i);
145 final float bi = actual.get(b0 + i);
146 final float daibi = Math.abs(ai - bi);
147 if( daibi > delta ) {
148 return;
149 }
150 }
151 throw new AssertionError(msg+"; Expected and actual are equal.");
152 }
153
154 public static void assertMatrix4fEquals(final Matrix4f expected, final Matrix4f actual, final float delta) {
155 assertMatrix4fEquals(null, expected, actual, delta);
156 }
157 public static void assertMatrix4fEquals(final String errmsg, final Matrix4f expected, final Matrix4f actual, final float delta) {
158 if(null == expected && null == actual) {
159 return;
160 }
161 final String msg = null != errmsg ? errmsg + " " : "";
162 if(null == expected) {
163 throw new AssertionError(msg+"; Expected is null, but actual not: "+actual);
164 }
165 if(null == actual) {
166 throw new AssertionError(msg+"; Actual is null, but expected not: "+expected);
167 }
168 for(int i=0; i<16; i++) {
169 final float ai = expected.get(i);
170 final float bi = actual.get(i);
171 final float daibi = Math.abs(ai - bi);
172 if( daibi > delta ) {
173 throw new AssertionError(msg+"; Expected @ ["+i+"] has "+ai+", but actual @ ["+i+"] has "+bi+", it's delta "+daibi+" > "+delta);
174 }
175 }
176 }
177 public static void assertMatrix4fNotEqual(final String errmsg, final Matrix4f expected, final Matrix4f actual, final float delta) {
178 if(null == expected || null == actual) {
179 return;
180 }
181 final String msg = null != errmsg ? errmsg + " " : "";
182 for(int i=0; i<16; i++) {
183 final float ai = expected.get(i);
184 final float bi = actual.get(i);
185 final float daibi = Math.abs(ai - bi);
186 if( daibi > delta ) {
187 return;
188 }
189 }
190 throw new AssertionError(msg+"; Expected and actual are equal.");
191 }
192
193 public static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value) {
194 try {
195 final Field f = instance.getClass().getField(fieldName);
196 if(value instanceof Boolean || f.getType().isInstance(value)) {
197 f.set(instance, value);
198 return true;
199 } else {
200 System.out.println(instance.getClass()+" '"+fieldName+"' field not assignable with "+value.getClass()+", it's a: "+f.getType());
201 }
202 } catch (final IllegalAccessException ex) {
203 throw new RuntimeException(ex);
204 } catch (final NoSuchFieldException nsfe) {
205 // OK - throw new RuntimeException(instance.getClass()+" has no '"+fieldName+"' field", nsfe);
206 }
207 return false;
208 }
209
210 public static class StreamDump extends InterruptSource.Thread {
211 final InputStream is;
212 final StringBuilder outString;
213 final OutputStream outStream;
214 final String prefix;
215 final Object sync;
216 volatile boolean eos = false;
217
218 public StreamDump(final OutputStream out, final String prefix, final InputStream is, final Object sync) {
219 this.is = is;
220 this.outString = null;
221 this.outStream = out;
222 this.prefix = prefix;
223 this.sync = sync;
224 }
225 public StreamDump(final StringBuilder sb, final String prefix, final InputStream is, final Object sync) {
226 this.is = is;
227 this.outString = sb;
228 this.outStream = null;
229 this.prefix = prefix;
230 this.sync = sync;
231 }
232 public StreamDump(final StringBuilder sb, final InputStream is, final Object sync) {
233 this.is = is;
234 this.outString = sb;
235 this.outStream = null;
236 this.prefix = null;
237 this.sync = sync;
238 }
239
240 public final boolean eos() { return eos; }
241
242 @Override
243 public void run() {
244 synchronized ( sync ) {
245 try {
246 final BufferedReader in = new BufferedReader( new InputStreamReader(is) );
247 String line = null;
248 while ((line = in.readLine()) != null) {
249 if( null != outString ) {
250 outString.append(line).append(Platform.getNewline());
251 } else if( null != outStream ) {
252 if( null != prefix ) {
253 outStream.write(prefix.getBytes());
254 }
255 outStream.write(line.getBytes());
256 outStream.write(Platform.getNewline().getBytes());
257 outStream.flush();
258 }
259 }
260 } catch (final IOException ioe) {
261 System.err.println("Caught "+ioe.getClass().getName()+": "+ioe.getMessage());
262 ioe.printStackTrace();
263 } finally {
264 eos = true;
265 sync.notifyAll();
266 }
267 }
268 }
269 }
270
271 public static void dumpSharedGLContext(final String prefix, final GLContext self) {
272 int i = 0, j = 0;
273 final GLContext master = self.getSharedMaster();
274 final int masterHash = null != master ? master.hashCode() : 0;
275 System.err.println(prefix+": hash 0x"+Integer.toHexString(self.hashCode())+", \t(isShared "+self.isShared()+", created "+self.isCreated()+", master 0x"+Integer.toHexString(masterHash)+")");
276 {
277 final List<GLContext> set = self.getCreatedShares();
278 for (final Iterator<GLContext> iter = set.iterator(); iter.hasNext(); ) {
279 final GLContext c = iter.next();
280 System.err.println(" Created Ctx #"+(i++)+": hash 0x"+Integer.toHexString(c.hashCode())+", \t(created "+c.isCreated()+")");
281 }
282 }
283 {
284 final List<GLContext> set = self.getDestroyedShares();
285 for (final Iterator<GLContext> iter = set.iterator(); iter.hasNext(); ) {
286 final GLContext c = iter.next();
287 System.err.println(" Destroyed Ctx #"+(j++)+": hash 0x"+Integer.toHexString(c.hashCode())+", \t(created "+c.isCreated()+")");
288 }
289 }
290 System.err.println("\t Total created "+i+" + destroyed "+j+" = "+(i+j));
291 System.err.println();
292 }
293}
294
295
296
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
Definition: Matrix4f.java:89
float get(final int i)
Gets the ith component, 0 <= i < 16.
Definition: Matrix4f.java:279
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
final boolean isCreated()
Indicates whether the underlying native OpenGL context has been created.
Definition: GLContext.java:604
final GLContext getSharedMaster()
Returns the shared master GLContext of this GLContext if shared, otherwise return null.
Definition: GLContext.java:272
final List< GLContext > getDestroyedShares()
Returns a new list of destroyed GLContext shared with this GLContext.
Definition: GLContext.java:282
final List< GLContext > getCreatedShares()
Returns a new list of created GLContext shared with this GLContext.
Definition: GLContext.java:277
StreamDump(final StringBuilder sb, final InputStream is, final Object sync)
Definition: MiscUtils.java:232
StreamDump(final OutputStream out, final String prefix, final InputStream is, final Object sync)
Definition: MiscUtils.java:218
StreamDump(final StringBuilder sb, final String prefix, final InputStream is, final Object sync)
Definition: MiscUtils.java:225
static void assertMatrix4fEquals(final Matrix4f expected, final Matrix4f actual, final float delta)
Definition: MiscUtils.java:154
static String toHexString(final short hex)
Definition: MiscUtils.java:88
static float atof(final String str, final float def)
Definition: MiscUtils.java:75
static String toHexString(final long hex)
Definition: MiscUtils.java:96
static boolean setFieldIfExists(final Object instance, final String fieldName, final Object value)
Definition: MiscUtils.java:193
static void assertMatrix4fEquals(final String errmsg, final Matrix4f expected, final Matrix4f actual, final float delta)
Definition: MiscUtils.java:157
static void waitForKey(final String preMessage)
Definition: MiscUtils.java:100
static void assertMatrix4fNotEqual(final String errmsg, final Matrix4f expected, final Matrix4f actual, final float delta)
Definition: MiscUtils.java:177
static void assertFloatBufferEquals(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta)
Definition: MiscUtils.java:108
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
static String toHexString(final int hex)
Definition: MiscUtils.java:92
static String toHexString(final byte hex)
Definition: MiscUtils.java:84
static boolean atob(final String str, final boolean def)
Definition: MiscUtils.java:48
static void assertFloatBufferNotEqual(final String errmsg, final FloatBuffer expected, final FloatBuffer actual, final float delta)
Definition: MiscUtils.java:133
static void dumpSharedGLContext(final String prefix, final GLContext self)
Definition: MiscUtils.java:271