Skip to content
Snippets Groups Projects
Commit bbdd4bb0 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

move wrapper code into wrappers/ directory. Python wrappers will follow.

parent 3282f30f
Branches
Tags
No related merge requests found
Showing
with 2446 additions and 73 deletions
......@@ -1209,79 +1209,11 @@ if(ENABLE_WRAP_PYTHON)
endif(SWIG_FOUND AND PYTHONLIBS_FOUND)
endif(ENABLE_WRAP_PYTHON)
if(ENABLE_WRAP_JAVA)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/org/geuz/gmsh/utils)
# flag used to compilation during Java wrapping
set(CMAKE_CXX_FLAGS "-fno-strict-aliasing")
# find SWIG and include the swig file
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
# find Java and JNI and include files required
find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
include_directories(${JAVA_INCLUDE_PATH})
include_directories(${JAVA_INCLUDE_PATH2})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# define where the interface file (used by SWIG) are
set(SWIG_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/WrapGmsh.i)
# let swig know that example.i is c++
set_source_files_properties(${SWIG_SOURCES} PROPERTIES CPLUSPLUS ON)
# define where the Java files generated by SWIG will be stored
set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/org/geuz/gmsh/generated/)
# defines the packaging of Java files generated by SWIG
set(JAVAPACKAGENAME "org.geuz.gmsh.generated")
set(CMAKE_SWIG_FLAGS -package ${JAVAPACKAGENAME})
# create the swig module called WrapGmsh
# using the SWIG_SOURCE flag defined previously
# swig will be used to create WrapGmshJAVA_wrap.cxx from SWIG_SOURCE
swig_add_module(WrapGmsh java ${SWIG_SOURCES})
# link dynamic library called libWrapGmsh with the help of "shared" library
swig_link_libraries(WrapGmsh shared)
# define a good name for the dynamic libraty which depends on the OS
if(WIN32)
set_target_properties(WrapGmsh PROPERTIES PREFIX "")
endif(WIN32)
if(UNIX)
set_target_properties(WrapGmsh PROPERTIES PREFIX "lib")
endif(UNIX)
add_custom_command(TARGET gmsh
POST_BUILD # do the rest of the command after the build period
# write on the output consol
COMMAND cmake -E echo "Compiling Java files..."
#compilation of Java files generated by SWIG
COMMAND ${JAVA_COMPILE}
${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/org/geuz/gmsh/generated/*.java
-classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
-d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# compilation of Java files used by wrapping
COMMAND ${JAVA_COMPILE}
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/org/geuz/gmsh/utils/*.java
-classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
-d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# compilation of Java files used as demonstration
COMMAND ${JAVA_COMPILE}
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/com/artenum/sample/*.java
-classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
-d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# create a jar file which contains the files with a .class extension
COMMAND cmake -E echo "Creating jar file..."
COMMAND ${JAVA_ARCHIVE} cvf ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/WrapGmsh.jar
-C ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/ org
COMMAND ${JAVA_ARCHIVE} cvf ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/Sample.jar
-C ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/ com
# create a jar from Sample
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/t5.geo
${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# copy the build.xml file to run the ant script
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/build.xml
${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/)
install(TARGETS WrapGmsh shared DESTINATION lib OPTIONAL)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/WrapGmsh.jar
DESTINATION lib)
if(ENABLE_WRAP_JAVA)
if(NOT HAVE_BLAS OR NOT HAVE_LAPACK)
message("WARNING: Java wrapping samples will not work without BLAS and LAPACK")
endif(NOT HAVE_BLAS OR NOT HAVE_LAPACK)
add_subdirectory(wrappers/java)
endif(ENABLE_WRAP_JAVA)
include(CPack)
......
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/org/geuz/gmsh/utils)
# flag used to compilation during Java wrapping
set(CMAKE_CXX_FLAGS "-fno-strict-aliasing")
# find SWIG and include the swig file
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
# find Java and JNI and include files required
find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
include_directories(${JAVA_INCLUDE_PATH})
include_directories(${JAVA_INCLUDE_PATH2})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# define where the interface file (used by SWIG) are
set(SWIG_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/WrapGmsh.i)
# let swig know that example.i is c++
set_source_files_properties(${SWIG_SOURCES} PROPERTIES CPLUSPLUS ON)
# define where the Java files generated by SWIG will be stored
set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/org/geuz/gmsh/generated/)
# defines the packaging of Java files generated by SWIG
set(JAVAPACKAGENAME "org.geuz.gmsh.generated")
set(CMAKE_SWIG_FLAGS -package ${JAVAPACKAGENAME})
# create the swig module called WrapGmsh
# using the SWIG_SOURCE flag defined previously
# swig will be used to create WrapGmshJAVA_wrap.cxx from SWIG_SOURCE
swig_add_module(WrapGmsh java ${SWIG_SOURCES})
swig_link_libraries(WrapGmsh shared)
# link dynamic library called libWrapGmsh with the help of "sharedSrc" library
# define a good name for the dynamic libraty which depends on the OS
if(WIN32)
set_target_properties(WrapGmsh PROPERTIES PREFIX "")
endif(WIN32)
if(UNIX)
set_target_properties(WrapGmsh PROPERTIES PREFIX "lib")
endif(UNIX)
add_custom_command(TARGET WrapGmsh
POST_BUILD # do the rest of the command after the build period
# write on the output consol
COMMAND cmake -E echo "Compiling Java files..."
#compilation of Java files generated by SWIG
COMMAND ${JAVA_COMPILE}
${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/org/geuz/gmsh/generated/*.java
-classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
-d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# compilation of Java files used by wrapping
COMMAND ${JAVA_COMPILE}
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/org/geuz/gmsh/utils/*.java
-classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
-d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# compilation of Java files used as demonstration
COMMAND ${JAVA_COMPILE}
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/com/artenum/sample/*.java
-classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
-d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# create a jar file which contains the files with a .class extension
COMMAND cmake -E echo "Creating jar file..."
COMMAND ${JAVA_ARCHIVE} cvf ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/WrapGmsh.jar
-C ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/ org
COMMAND ${JAVA_ARCHIVE} cvf ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/Sample.jar
-C ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/ com
# create a jar from Sample
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/t5.geo
${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
# copy the build.xml file to run the ant script
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/build.xml
${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/)
install(TARGETS WrapGmsh DESTINATION lib OPTIONAL)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/WrapGmsh.jar
DESTINATION lib)
/*wrapGmsh.i*/
%module WrapGmsh
%{
/* Put header files here or function declarations like below */
#include "Gmsh.h"
#include "GModel.h"
#include "Geo.h"
#include "GVertex.h"
#include "gmshVertex.h"
#include "GEntity.h"
#include "GModelFactory.h"
#include "GEdge.h"
#include "GFace.h"
#include "GRegion.h"
#include "GeoStringInterface.h"
#include "GEntity.h"
#include "setWrapperGmsh.h"
#include "listWrapperGmsh.h"
#include "MPoint.h"
#include "MLine.h"
#include "MTriangle.h"
#include "MFace.h"
#include "MTetrahedron.h"
#include "GModelIO_Mesh.cpp"
%}
//declares meshGenerationStatus enumeration
%include "enums.swg"
%javaconst(1);
enum meshGenerationStatus {PENDING, DONE, FAILED};
//these fonctions/classes must be wrapped
%include "std_string.i"
%include "MElement.h"
%include "MPoint.h"
%include "MLine.h"
%include "MFace.h"
%include "MTriangle.h"
%include "MTetrahedron.h"
%include "GEntity.h"
%include "GModel.h"
%include "Geo.h"
%include "GVertex.h"
%include "gmshVertex.h"
%include "GModelFactory.h"
%include "GEdge.h"
%include "GFace.h"
%include "GRegion.h"
%include "MVertex.h"
%include "GEntity.h"
%include "ListUtils.h"
extern int GModel::getNumElementsMSH(GModel *m, bool saveAll, int saveSinglePartition);
extern int GModel::indexMeshVertices(bool all, int singlePartition);
extern int GmshInitialize(int argc, char **argv);
extern int GmshFinalize();
extern Vertex *Create_Vertex(int Num, double X, double Y, double Z, double lc, double u);
extern void add_physical(std::string type, List_T *list, std::string fileName);
//use the Vector object defined in Java language to wrap the std vector.
%include "std_vector.i"
namespace std {
%template(EdgeVector) std::vector<GEdge *>;
%template(EdgeVectorOfVector) std::vector<std::vector<GEdge *> >;
%template(FaceVector) std::vector<GFace *>;
%template(FaceVectorOfVector) std::vector<std::vector<GFace *> >;
%template(GVertexVector) std::vector<GVertex *>;
%template(RegionVector) std::vector<GRegion *>;
%template(GEntityVector) std::vector<GEntity*>;
%template(MVertexVector) std::vector<MVertex*>;
%template(MPointVector) std::vector<MPoint*>;
%template(MLineVector) std::vector<MLine*>;
%template(MTriangleVector) std::vector<MTriangle*>;
%template(MTetrahedrVector) std::vector<MTetrahedron*>;
%template(IntVector) std::vector<int>;
};
//wrap Set from std
%include "setWrapperGmsh.h"
%template (GVertexSetWrapper) SetWrapperGmsh<GVertex*, GEntityLessThan>;
%template (GVertexSetIterator) SetIteratorGmsh<GVertex*, GEntityLessThan>;
%template (GEdgeSetWrapper) SetWrapperGmsh<GEdge*, GEntityLessThan>;
%template (GEdgeSetIterator) SetIteratorGmsh<GEdge*, GEntityLessThan>;
%template (GFaceSetWrapper) SetWrapperGmsh<GFace*, GEntityLessThan>;
%template (GFaceSetIterator) SetIteratorGmsh<GFace*, GEntityLessThan>;
%template (GRegionSetWrapper) SetWrapperGmsh<GRegion*, GEntityLessThan>;
%template (GRegionSetIterator) SetIteratorGmsh<GRegion*, GEntityLessThan>;
//wrap List from std
%include "listWrapperGmsh.h"
%template (GVertexListWrapper) ListWrapperGmsh<GVertex*>;
%template (GVertexListIterator) ListIteratorGmsh<GVertex*>;
%template (GEdgeListWrapper) ListWrapperGmsh<GEdge*>;
%template (GEdgeListIterator) ListIteratorGmsh<GEdge*>;
%template (GFaceListWrapper) ListWrapperGmsh<GFace*>;
%template (GFaceListIterator) ListIteratorGmsh<GFace*>;
%template (GRegionListWrapper) ListWrapperGmsh<GRegion*>;
%template (GRegionListIterator) ListIteratorGmsh<GRegion*>;
<project name="GMSH wrapping sample" default="help" basedir=".">
<!-- ////////////////////////////////////////////////////////////// -->
<!-- Global properties -->
<!-- ////////////////////////////////////////////////////////////// -->
<!-- Build year -->
<tstamp>
<format property="year" pattern="yyyy" />
</tstamp>
<!-- ////////////////////////////////////////////////////////////// -->
<!-- System-specific properties -->
<!-- ////////////////////////////////////////////////////////////// -->
<!-- System architecture -->
<condition property="arch" value="32b">
<or>
<os arch="x86" />
<os arch="i386" />
<os arch="i486" />
<os arch="i586" />
</or>
</condition>
<condition property="arch" value="64b">
<or>
<os arch="x86_64" />
<os arch="i686" />
<os arch="amd64" />
</or>
</condition>
<!-- Operating system -->
<condition property="platform" value="linux${arch}">
<os name="Linux" />
</condition>
<condition property="platform" value="win32b">
<os family="Windows" />
</condition>
<condition property="platform" value="osx64b">
<os name="Mac OS X" />
</condition>
<!-- Native environment variable name -->
<condition property="jni.system.var" value="LD_LIBRARY_PATH">
<os name="Linux" />
</condition>
<condition property="jni.system.var" value="PATH">
<os family="Windows" />
</condition>
<condition property="jni.system.var" value="DYLD_LIBRARY_PATH">
<os name="Mac OS X" />
</condition>
<!-- ////////////////////////////////////////////////////////////// -->
<!-- Classpath -->
<!-- ////////////////////////////////////////////////////////////// -->
<!-- Run classpath -->
<path id="run.class.path">
<!-- Generated JAR files -->
<fileset dir="${basedir}">
<include name="Sample.jar" />
<include name="WrapGmsh.jar" />
</fileset>
</path>
<!-- Main classes -->
<property name="sample1.main" value="com.artenum.sample.EssaiGmsh_v1" />
<property name="sample2.main" value="com.artenum.sample.EssaiGmsh_v2" />
<!-- ////////////////////////////////////////////////////////////// -->
<!-- Ant build message -->
<!-- ////////////////////////////////////////////////////////////// -->
<echo message="//////////////////////////////////////////////////////////////" />
<echo message=" GMSH wrapping sample " />
<echo message=" Version 0.0.1 " />
<echo message=" " />
<echo message="//////////////////////////////////////////////////////////////" />
<echo message=" Developed by Artenum SARL 2011-${year} " />
<echo message=" http://www.artenum.com " />
<echo message="//////////////////////////////////////////////////////////////" />
<echo message="" />
<echo message="System information:" />
<echo message="" />
<echo message="Architecture: ${os.arch}" />
<echo message="Operating system: ${os.name}" />
<echo message="Java version: ${java.version}" />
<!-- ////////////////////////////////////////////////////////////// -->
<!-- Ant help message -->
<!-- ////////////////////////////////////////////////////////////// -->
<target name="help" description="Display the main targets available">
<echo message="//////////////////////////////////////////////////////////////" />
<echo message="Ant tasks available:" />
<echo message=" " />
<echo message=" - clean : remove all Ant-generated files" />
<echo message=" " />
<echo message=" - sample1 : executes the GMSH wrapping sample code nr 1" />
<echo message=" - sample2 : executes the GMSH wrapping sample code nr 2" />
</target>
<!-- ////////////////////////////////////////////////////////////// -->
<!-- Cleaning Ant files -->
<!-- ////////////////////////////////////////////////////////////// -->
<target name="clean" description="Clean Ant-generated files">
<echo message="//////////////////////////////////////////////////////////////" />
<echo message=" Cleaning Ant-generated files" />
<echo message="//////////////////////////////////////////////////////////////" />
<echo message="" />
<delete file="device_with_booms.msh" />
<delete file="meshtest.msh" />
<delete file="test.geo" />
</target>
<!-- ////////////////////////////////////////////////////////////// -->
<!-- Sample codes execution -->
<!-- ////////////////////////////////////////////////////////////// -->
<!-- Execute the application -->
<target name="sample1" description="Execute the sample 1">
<echo message="##############################################################" />
<echo message=" Executing sample1" />
<echo message="##############################################################" />
<echo message="" />
<java classname="${sample1.main}" fork="true">
<env key="${jni.system.var}" value=".." />
<classpath refid="run.class.path" />
<jvmarg value="-Xmx512M" />
<jvmarg value="-Xms100M" />
</java>
</target>
<!-- Execute the application -->
<target name="sample2" description="Execute the sample 2">
<echo message="##############################################################" />
<echo message=" Executing sample2" />
<echo message="##############################################################" />
<echo message="" />
<java classname="${sample2.main}" fork="true">
<env key="${jni.system.var}" value=".." />
<classpath refid="run.class.path" />
<jvmarg value="-Xmx512M" />
<jvmarg value="-Xms100M" />
</java>
</target>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/home/benj/Bureau/gmshWrap_v1.6/build/WrappingJava/WrapGmsh.jar"/>
<classpathentry kind="output" path=""/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>java</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
This diff is collapsed.
/**
* Project : Java Wrapping of Gmsh
* Copyright : see Gmsh Copyright
* License : see Gmsh License
* Contrat number : internal Artenum development
* Developed by: Artenum SARL
* Creation : august 2011
* authors : Benjamin JEANTY-RUARD - Artenum
* Status : final
*/
package com.artenum.sample;
import org.geuz.gmsh.generated.GModel;
import org.geuz.gmsh.generated.WrapGmsh;
/**
* This class is an example of use of Gmsh wrapping in Java.
*
* @author benjamin jeanty-ruard
*
*/
public class EssaiGmsh_v2 {
static {
try {
System.loadLibrary("WrapGmsh");
} catch (final UnsatisfiedLinkError e) {
System.err
.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
public static void main(final String[] args) {
// initialization of gmsh
WrapGmsh.GmshInitialize(0, null);
final GModel m1 = new GModel();
// import information contained in .geo file inside GModel m1
GModel.readGEO("t5.geo");
// mesh the geometry defined in the GModel m
m1.mesh(3);
// write .msh file which contains informations about mesh in GModel m.
m1.writeMSH("t5.msh");
WrapGmsh.GmshFinalize();
}
}
package org.geuz.gmsh.utils;
import java.lang.reflect.Method;
import java.util.AbstractList;
import java.util.List;
/**
* Wrapper for std::vector from SWIG
*
* @author Chad Retz
*
* @param <T>
*/
public class NativeList<T> extends AbstractList<T> implements List<T> {
private final Object listWrapper;
private final Method sizeMethod;
private final Method addMethod;
private final Method clearMethod;
private final Method setMethod;
private final Method removeMethod;
private final Method getMethod;
/**
* Construct native list from std::vector wrappers
*
* @param nativeClass The native class
* @param nativeList The SWIG vector
* @param listWrapperClass The SWIG vector class wrapper
*/
public NativeList(Class<T> nativeClass, Object nativeList, Class<?> listWrapperClass) {
try {
listWrapper = listWrapperClass.getConstructor(nativeList.getClass()).
newInstance(nativeList);
sizeMethod = listWrapperClass.getDeclaredMethod("size");
addMethod = listWrapperClass.getDeclaredMethod("add",
Integer.TYPE, nativeClass);
clearMethod = listWrapperClass.getDeclaredMethod("clear");
setMethod = listWrapperClass.getDeclaredMethod("set",
Integer.TYPE, nativeClass);
removeMethod = listWrapperClass.getDeclaredMethod("remove", Integer.TYPE);
getMethod = listWrapperClass.getDeclaredMethod("get", Integer.TYPE);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void add(int index, T item) {
try {
addMethod.invoke(listWrapper, index, item);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void clear() {
try {
clearMethod.invoke(listWrapper);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
@SuppressWarnings("unchecked")
public T set(int index, T item) {
try {
return (T) setMethod.invoke(listWrapper, index, item);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
@SuppressWarnings("unchecked")
public T remove(int index) {
try {
return (T) removeMethod.invoke(listWrapper, index);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean remove(Object item) {
try {
return (Boolean) removeMethod.invoke(listWrapper, item);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
@SuppressWarnings("unchecked")
public T get(int index) {
try {
return (T) getMethod.invoke(listWrapper, index);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public int size() {
try {
return (Integer) sizeMethod.invoke(listWrapper);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package org.geuz.gmsh.utils;
import java.lang.reflect.Method;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* Wrapper for std::set and std::list from SWIG. This does not support removal
* during iteration.
*
* @author Chad Retz
*
* @param <T>
*/
public class NativeSet<T> extends AbstractSet<T> implements Set<T> {
private final Class<T> nativeClass;
private final Class<?> setIteratorClass;
private final Object nativeSet;
private final Object setWrapper;
private final Method sizeMethod;
private final Method containsMethod;
private final Method addMethod;
private final Method clearMethod;
private final Method removeMethod;
private final Method hasNextMethod;
private final Method nextMethod;
/**
* Instantiate the native set
*
* @param nativeClass
* The native class
* @param setIteratorClass
* The class for the SetIterator
* @param nativeSet
* The native set object
* @param setWrapperClass
* The class for the Set
*/
public NativeSet(final Class<T> nativeClass, final Class<?> setIteratorClass, final Object nativeSet, final Class<?> setWrapperClass) {
this.nativeClass = nativeClass;
this.setIteratorClass = setIteratorClass;
this.nativeSet = nativeSet;
try {
this.setWrapper = setWrapperClass.getConstructor(nativeSet.getClass()).newInstance(nativeSet);
this.sizeMethod = setWrapperClass.getDeclaredMethod("size");
this.containsMethod = setWrapperClass.getDeclaredMethod("contains", nativeClass);
this.addMethod = setWrapperClass.getDeclaredMethod("add", nativeClass);
this.clearMethod = setWrapperClass.getDeclaredMethod("clear");
this.removeMethod = setWrapperClass.getDeclaredMethod("remove", nativeClass);
this.hasNextMethod = setIteratorClass.getDeclaredMethod("hasNext");
this.nextMethod = setIteratorClass.getDeclaredMethod("next");
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean add(final T item) {
try {
return (Boolean) this.addMethod.invoke(this.setWrapper, item);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void clear() {
try {
this.clearMethod.invoke(this.setWrapper);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean contains(final Object item) {
try {
return this.nativeClass.isAssignableFrom(item.getClass()) && (Boolean) this.containsMethod.invoke(this.setWrapper, item);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
public Iterator<T> iterator() {
return new NativeSetIterator();
}
@Override
public boolean remove(final Object item) {
try {
return (Boolean) this.removeMethod.invoke(this.setWrapper, item);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean removeAll(final Collection<?> collection) {
boolean modified = false;
for (final Object item : collection) {
modified |= this.remove(item);
}
return modified;
}
@Override
public boolean retainAll(final Collection<?> collection) {
// best way?
final List<T> toRemove = new ArrayList<T>(this.size());
for (final T item : this) {
if (!collection.contains(item)) {
toRemove.add(item);
}
}
return this.removeAll(toRemove);
}
@Override
public int size() {
try {
return (Integer) this.sizeMethod.invoke(this.setWrapper);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
protected class NativeSetIterator implements Iterator<T> {
private final Object setIterator;
private NativeSetIterator() {
try {
this.setIterator = NativeSet.this.setIteratorClass.getConstructor(NativeSet.this.nativeSet.getClass()).newInstance(NativeSet.this.nativeSet);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean hasNext() {
try {
return (Boolean) NativeSet.this.hasNextMethod.invoke(this.setIterator);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
@Override
@SuppressWarnings("unchecked")
public T next() {
try {
return (T) NativeSet.this.nextMethod.invoke(this.setIterator);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
/**
* {@inheritDoc}
* <p>
* Unsupported
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}
#pragma once
#include <list>
template<class T>
class ListWrapperGmsh
{
public:
std::list<T>* _list;
ListWrapperGmsh(std::list<T>* original)
{
this->_list = original;
}
~ListWrapperGmsh()
{
}
int size()
{
return this->_list->size();
}
bool contains(T item)
{
typename std::list<T>::iterator iter;
for(iter = this->_list->begin();
iter != this->_list->end(); iter++) {
if (*iter == item) {
return true;
}
}
return false;
}
bool add(T item)
{
this->_list->push_back(item);
return true;
}
void clear()
{
this->_list->clear();
}
bool remove(T item)
{
int size = this->_list->size();
this->_list->remove(item);
return size != this->_list->size();
}
};
template<class T>
class ListIteratorGmsh
{
private:
std::list<T>* _list;
typename std::list<T>::const_iterator _iter;
public:
ListIteratorGmsh(std::list<T>* original)
{
this->_list = original;
this->_iter = this->_list->begin();
}
bool hasNext()
{
return this->_iter != this->_list->end();
}
T next()
{
T ret = (T) *this->_iter;
this->_iter++;
return ret;
}
};
#pragma once
#include <set>
template<class T, class U>
class SetWrapperGmsh
{
private:
std::set<T, U>* _set;
public:
// std::set<T>::iterator iterType;
SetWrapperGmsh(std::set<T, U>* original)
{
this->_set = original;
}
~SetWrapperGmsh()
{
}
int size()
{
return this->_set->size();
}
bool contains(T item)
{
// std::set<T>::iterator iter= this->_set->find(item);
return this->_set->find(item) != this->_set->end();
}
bool add(T item)
{
return this->_set->insert(item).second;
}
void clear()
{
this->_set->clear();
}
bool remove(T item)
{
if (this->_set->find(item) != this->_set->end()) {
this->_set->erase(this->_set->find(item));
return true;
} else {
return false;
}
}
};
template<class T, class U>
class SetIteratorGmsh
{
private:
std::set<T, U>* _set;
typename std::set<T, U>::iterator _iter;
public:
SetIteratorGmsh(std::set<T, U>* original)
{
this->_set = original;
this->_iter = this->_set->begin();
}
bool hasNext()
{
return this->_iter != this->_set->end();
}
T next()
{
T ret = (T) *this->_iter;
this->_iter++;
return ret;
}
};
/*********************************************************************
*
* Gmsh tutorial 5
*
* Characteristic lengths, arrays of variables, functions, loops
*
*********************************************************************/
// We start by defining some target mesh sizes:
lcar1 = .1;
lcar2 = .0005;
lcar3 = .055;
// If we wanted to change these mesh sizes globally (without changing
// the above definitions), we could give a global scaling factor for
// all characteristic lengths on the command line with the `-clscale'
// option (or with `Mesh.CharacteristicLengthFactor' in an option
// file). For example, with:
//
// > gmsh t5.geo -clscale 1
//
// this input file produces a mesh of approximately 1,300 nodes and
// 11,000 tetrahedra. With
//
// > gmsh t5.geo -clscale 0.2
//
// the mesh counts approximately 350,000 nodes and 2.1 million
// tetrahedra. You can check mesh statistics in the graphical user
// interface with the `Tools->Statistics' menu.
// We proceed by defining some elementary entities describing a
// truncated cube:
Point(1) = {0.5,0.5,0.5,lcar2}; Point(2) = {0.5,0.5,0,lcar1};
Point(3) = {0,0.5,0.5,lcar1}; Point(4) = {0,0,0.5,lcar1};
Point(5) = {0.5,0,0.5,lcar1}; Point(6) = {0.5,0,0,lcar1};
Point(7) = {0,0.5,0,lcar1}; Point(8) = {0,1,0,lcar1};
Point(9) = {1,1,0,lcar1}; Point(10) = {0,0,1,lcar1};
Point(11) = {0,1,1,lcar1}; Point(12) = {1,1,1,lcar1};
Point(13) = {1,0,1,lcar1}; Point(14) = {1,0,0,lcar1};
Line(1) = {8,9}; Line(2) = {9,12}; Line(3) = {12,11};
Line(4) = {11,8}; Line(5) = {9,14}; Line(6) = {14,13};
Line(7) = {13,12}; Line(8) = {11,10}; Line(9) = {10,13};
Line(10) = {10,4}; Line(11) = {4,5}; Line(12) = {5,6};
Line(13) = {6,2}; Line(14) = {2,1}; Line(15) = {1,3};
Line(16) = {3,7}; Line(17) = {7,2}; Line(18) = {3,4};
Line(19) = {5,1}; Line(20) = {7,8}; Line(21) = {6,14};
Line Loop(22) = {-11,-19,-15,-18}; Plane Surface(23) = {22};
Line Loop(24) = {16,17,14,15}; Plane Surface(25) = {24};
Line Loop(26) = {-17,20,1,5,-21,13}; Plane Surface(27) = {26};
Line Loop(28) = {-4,-1,-2,-3}; Plane Surface(29) = {28};
Line Loop(30) = {-7,2,-5,-6}; Plane Surface(31) = {30};
Line Loop(32) = {6,-9,10,11,12,21}; Plane Surface(33) = {32};
Line Loop(34) = {7,3,8,9}; Plane Surface(35) = {34};
Line Loop(36) = {-10,18,-16,-20,4,-8}; Plane Surface(37) = {36};
Line Loop(38) = {-14,-13,-12,19}; Plane Surface(39) = {38};
// Instead of using included files, we now use a user-defined function
// in order to carve some holes in the cube:
Function CheeseHole
// In the following commands we use the reserved variable name
// `newp', which automatically selects a new point number. This
// number is chosen as the highest current point number, plus
// one. (Note that, analogously to `newp', the variables `newc',
// `news', `newv' and `newreg' select the highest number amongst
// currently defined curves, surfaces, volumes and `any entities
// other than points', respectively.)
p1 = newp; Point(p1) = {x, y, z, lcar3} ;
p2 = newp; Point(p2) = {x+r,y, z, lcar3} ;
p3 = newp; Point(p3) = {x, y+r,z, lcar3} ;
p4 = newp; Point(p4) = {x, y, z+r,lcar3} ;
p5 = newp; Point(p5) = {x-r,y, z, lcar3} ;
p6 = newp; Point(p6) = {x, y-r,z, lcar3} ;
p7 = newp; Point(p7) = {x, y, z-r,lcar3} ;
c1 = newreg; Circle(c1) = {p2,p1,p7};
c2 = newreg; Circle(c2) = {p7,p1,p5};
c3 = newreg; Circle(c3) = {p5,p1,p4};
c4 = newreg; Circle(c4) = {p4,p1,p2};
c5 = newreg; Circle(c5) = {p2,p1,p3};
c6 = newreg; Circle(c6) = {p3,p1,p5};
c7 = newreg; Circle(c7) = {p5,p1,p6};
c8 = newreg; Circle(c8) = {p6,p1,p2};
c9 = newreg; Circle(c9) = {p7,p1,p3};
c10 = newreg; Circle(c10) = {p3,p1,p4};
c11 = newreg; Circle(c11) = {p4,p1,p6};
c12 = newreg; Circle(c12) = {p6,p1,p7};
// We need non-plane surfaces to define the spherical holes. Here we
// use ruled surfaces, which can have 3 or 4 sides:
l1 = newreg; Line Loop(l1) = {c5,c10,c4}; Ruled Surface(newreg) = {l1};
l2 = newreg; Line Loop(l2) = {c9,-c5,c1}; Ruled Surface(newreg) = {l2};
l3 = newreg; Line Loop(l3) = {c12,-c8,-c1}; Ruled Surface(newreg) = {l3};
l4 = newreg; Line Loop(l4) = {c8,-c4,c11}; Ruled Surface(newreg) = {l4};
l5 = newreg; Line Loop(l5) = {-c10,c6,c3}; Ruled Surface(newreg) = {l5};
l6 = newreg; Line Loop(l6) = {-c11,-c3,c7}; Ruled Surface(newreg) = {l6};
l7 = newreg; Line Loop(l7) = {-c2,-c7,-c12};Ruled Surface(newreg) = {l7};
l8 = newreg; Line Loop(l8) = {-c6,-c9,c2}; Ruled Surface(newreg) = {l8};
// We then store the surface loops identification numbers in a list
// for later reference (we will need these to define the final
// volume):
theloops[t] = newreg ;
Surface Loop(theloops[t]) = {l8+1,l5+1,l1+1,l2+1,l3+1,l7+1,l6+1,l4+1};
thehole = newreg ;
Volume(thehole) = theloops[t] ;
Return
// We can use a `For' loop to generate five holes in the cube:
x = 0 ; y = 0.75 ; z = 0 ; r = 0.09 ;
For t In {1:5}
x += 0.166 ;
z += 0.166 ;
// We call the `CheeseHole' function:
Call CheeseHole ;
// We define a physical volume for each hole:
Physical Volume (t) = thehole ;
// We also print some variables on the terminal (note that, since
// all variables are treated internally as floating point numbers,
// the format string should only contain valid floating point format
// specifiers like `%g', `%f', '%e', etc.):
Printf("Hole %g (center = {%g,%g,%g}, radius = %g) has number %g!",
t, x, y, z, r, thehole) ;
EndFor
// We can then define the surface loop for the exterior surface of the
// cube:
theloops[0] = newreg ;
Surface Loop(theloops[0]) = {35,31,29,37,33,23,39,25,27} ;
// The volume of the cube, without the 5 holes, is now defined by 6
// surface loops: the first surface loop defines the exterior surface;
// the surface loops other than the first one define holes. (Again,
// to reference an array of variables, its identifier is followed by
// square brackets):
Volume(186) = {theloops[]} ;
// We finally define a physical volume for the elements discretizing
// the cube, without the holes (whose elements were already tagged
// with numbers 1 to 5 in the `For' loop):
Physical Volume (10) = 186 ;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment