Skip to content

Commit

Permalink
Merge pull request #919 from vorth/document-exporters
Browse files Browse the repository at this point in the history
Porting POV-Ray and other exporters
  • Loading branch information
vorth authored Sep 13, 2024
2 parents 50b1682 + a142589 commit 43bdc5e
Show file tree
Hide file tree
Showing 21 changed files with 1,285 additions and 247 deletions.
2 changes: 1 addition & 1 deletion cicd/jsweet-legacy-code.bash
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ banner 'Transpiling core Java sources with JSweet' #############################
./gradlew --continue -p online coreClean core &> core-errors.txt # ignore the exit code, it always fails
cat core-errors.txt

grep -q 'transpilation failed with 33 error(s) and 0 warning(s)' core-errors.txt \
grep -q 'transpilation failed with 35 error(s) and 0 warning(s)' core-errors.txt \
&& banner 'JSweet core transpile found the expected errors' \
|| { banner 'UNEXPECTED CHANGE IN JSWEET CORE ERRORS'; exit 1; }

Expand Down
17 changes: 3 additions & 14 deletions cicd/online.bash
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,16 @@ buildForProduction() {
}

prepareJSweet(){
banner 'We are no longer using JSweet. See online/build.gradle for more details.'
banner 'JSweet no longer builds successfully. See online/build.gradle for more details.'
# source cicd/prepare-jsweet.bash || exit $?
}

jsweet(){
banner 'We are no longer using JSweet. See online/build.gradle for more details.'
# source cicd/jsweet-legacy-code.bash || exit $?
source cicd/jsweet-legacy-code.bash || exit $?
}

devJava(){
jsweet || exit $?

cd online

marshallResources || exit $?

generateRevisionJs || exit $?

installJsDependencies || exit $?

yarn run dev
banner 'We are no longer using JSweet automatically. You can still use the "jsweet" command here manually.'
}

devJs(){
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/com/vzome/core/editor/DocumentModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.vzome.core.editor.api.EditorModel;
import com.vzome.core.editor.api.OrbitSource;
import com.vzome.core.editor.api.UndoableEdit;
import com.vzome.core.exporters.DocumentIntf;
import com.vzome.core.exporters.ShapesJsonExporter;
import com.vzome.core.math.Projection;
import com.vzome.core.math.RealVector;
Expand All @@ -65,10 +66,11 @@
import com.vzome.core.render.RenderedModel;
import com.vzome.core.tools.BookmarkToolFactory;
import com.vzome.core.viewing.Camera;
import com.vzome.core.viewing.CameraIntf;
import com.vzome.core.viewing.Lights;
import com.vzome.xml.DomSerializer;

public class DocumentModel implements Snapshot .Recorder, Context
public class DocumentModel implements Snapshot .Recorder, Context, DocumentIntf
{
private final RealizedModelImpl mRealizedModel;

Expand Down Expand Up @@ -875,6 +877,12 @@ public Camera getCamera()
return this .defaultCamera;
}

@Override
public CameraIntf getCameraModel()
{
return this .defaultCamera;
}

public Segment getSelectedSegment()
{
return (Segment) editorModel .getSelectedConstruction( Segment.class );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
import java.io.File;
import java.io.Writer;

import com.vzome.core.editor.DocumentModel;
import com.vzome.core.viewing.Camera;
import com.vzome.core.viewing.CameraIntf;
import com.vzome.core.viewing.Lights;

public abstract class DocumentExporter extends GeometryExporter
{
protected transient Lights mLights;
protected transient Camera mScene;
protected transient CameraIntf mScene;

/**
* Subclasses can override this if they need to export history, the lesson model, or the selection.
*/
public void exportDocument( DocumentModel doc, File file, Writer writer, int height, int width ) throws Exception
public void exportDocument( DocumentIntf doc, File file, Writer writer, int height, int width ) throws Exception
{
mScene = doc .getCamera();
mScene = doc .getCameraModel();
mLights = doc .getSceneLighting();
this .exportGeometry( doc .getRenderedModel(), file, writer, height, width );
mScene = null;
Expand Down
25 changes: 25 additions & 0 deletions core/src/main/java/com/vzome/core/exporters/DocumentIntf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.vzome.core.exporters;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.vzome.core.editor.ToolsModel;
import com.vzome.core.editor.api.EditorModel;
import com.vzome.core.render.RenderedModel;
import com.vzome.core.viewing.CameraIntf;
import com.vzome.core.viewing.Lights;

public interface DocumentIntf
{
CameraIntf getCameraModel();

Lights getSceneLighting();

RenderedModel getRenderedModel();

ToolsModel getToolsModel();

Element getDetailsXml( Document dom, boolean b );

EditorModel getEditorModel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import com.vzome.core.model.Panel;
import com.vzome.core.model.Strut;
import com.vzome.core.render.RenderedManifestation;
import com.vzome.core.viewing.Camera;
import com.vzome.core.viewing.CameraIntf;

// Combines features from DaeExporter and OffExporter
public class GeoGebraExporter extends DocumentExporter
Expand Down Expand Up @@ -205,7 +205,7 @@ private Element getTemplate(String expression, Element parent) throws XPathExpre
return template;
}

private void setScene(Camera scene, Color color) throws XPathExpressionException {
private void setScene(CameraIntf scene, Color color) throws XPathExpressionException {
Element euclidianView3DNode = (Element) xpath.evaluate("//geogebra//euclidianView3D",
doc.getDocumentElement(), XPathConstants.NODE);
{ // coordSystem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ protected String getBoilerplate( String resourcePath )
return ResourceLoader .loadStringResource( resourcePath );
}

/**
* Subclasses can override this if they need to export history, the lesson model, or the selection.
*/
public void exportGeometry( RenderedModel model, File file, Writer writer, int height, int width ) throws Exception
{
mModel = model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.vzome.core.editor.DocumentModel;
import com.vzome.xml.DomSerializer;

public class HistoryExporter extends DocumentExporter
{
@Override
public void exportDocument( DocumentModel document, File file, Writer writer, int height, int width ) throws Exception
public void exportDocument( com.vzome.core.exporters.DocumentIntf document, File file, Writer writer, int height, int width ) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance();
factory .setNamespaceAware( true );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.vzome.core.construction.Construction;
import com.vzome.core.construction.Point;
import com.vzome.core.construction.Polygon;
import com.vzome.core.editor.DocumentModel;
import com.vzome.core.editor.Tool;
import com.vzome.core.editor.ToolsModel;
import com.vzome.core.math.RealVector;
Expand All @@ -37,7 +36,7 @@
public class OpenScadExporter extends DocumentExporter
{
@Override
public void exportDocument( DocumentModel doc, File file, Writer writer, int height, int width ) throws Exception
public void exportDocument( DocumentIntf doc, File file, Writer writer, int height, int width ) throws Exception
{
ToolsModel toolsModel = doc .getToolsModel();
super .mModel = doc .getRenderedModel();
Expand Down
38 changes: 9 additions & 29 deletions core/src/main/java/com/vzome/core/exporters/POVRayExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
import java.util.Locale;
import java.util.Map;

import javax.vecmath.Matrix4f;
import javax.vecmath.Tuple3f;
import javax.vecmath.Vector3f;

import com.vzome.core.algebra.AlgebraicField;
import com.vzome.core.algebra.AlgebraicMatrix;
import com.vzome.core.algebra.AlgebraicVector;
Expand All @@ -26,7 +22,7 @@
import com.vzome.core.math.RealVector;
import com.vzome.core.math.symmetry.Embedding;
import com.vzome.core.render.RenderedManifestation;
import com.vzome.core.viewing.Camera;
import com.vzome.core.viewing.CameraIntf;

/**
* Renders out to POV-Ray using #declare statements to reuse geometry.
Expand All @@ -38,12 +34,8 @@ public class POVRayExporter extends DocumentExporter

private static final String PREAMBLE_FILE = "com/vzome/core/exporters/povray/preamble.pov";

public void mapViewToWorld( Camera view, Vector3f vector )
public void mapViewToWorld( CameraIntf view, RealVector vector )
{
Matrix4f viewTrans = new Matrix4f();
view .getViewTransform( viewTrans );
viewTrans .invert();
viewTrans .transform( vector );
}

@Override
Expand All @@ -56,9 +48,8 @@ public void doExport( File povFile, Writer writer, int height, int width ) throw
{
output = new PrintWriter( writer );

Vector3f lookDir = new Vector3f(), upDir = new Vector3f(), rightDir = new Vector3f();
mScene .getViewOrientation( lookDir, upDir );
rightDir .cross( lookDir, upDir );
RealVector lookDir = mScene .getLookDirectionRV();
RealVector upDir = mScene .getUpDirectionRV();
FORMAT .setMaximumFractionDigits( 8 );

output .println();
Expand All @@ -67,23 +58,13 @@ public void doExport( File povFile, Writer writer, int height, int width ) throw
output .println();
output .println( "#declare up_dir = " + printTuple3d( upDir ) + ";" );
output .println();
output .println( "#declare right_dir = " + printTuple3d( rightDir ) + ";" );
output .println();
output .println( "#declare viewpoint_distance = " + mScene .getViewDistance() + ";" );
output .println();
output .println( "#declare near_clip_distance = " + mScene .getNearClipDistance() + ";" );
output .println();
output .println( "#declare far_clip_distance = " + mScene .getFarClipDistance() + ";" );
output .println();
output .println( "#declare look_at_point = " + printTuple3d( mScene .getLookAtPoint() ) + ";" );
output .println( "#declare look_at_point = " + printTuple3d( mScene .getLookAtPointRV() ) + ";" );
output .println();
output .println( "#declare field_of_view = " + mScene .getFieldOfView() + ";" );
output .println();
output .println( "#declare canvas_width = " + width + ";" );
output .println();
output .println( "#declare canvas_height = " + height + ";" );
output .println();
output .println( "#declare parallel_proj = " + (mScene.isPerspective()?0:1) + ";" );
output .println( "#declare parallel_proj = " + (mScene .isPerspective()?0:1) + ";" );
output .println();

InputStream input = getClass() .getClassLoader()
Expand All @@ -103,9 +84,8 @@ public void doExport( File povFile, Writer writer, int height, int width ) throw
for ( int i = 0; i<3; i++ ) {
Color color = mLights .getDirectionalLightColor( i );
RealVector rv = mLights .getDirectionalLightVector( i );
Vector3f dir = new Vector3f( rv.x, rv.y, rv.z );
mapViewToWorld( mScene, dir );
output .print( "light_source { -light_distance * " + printTuple3d( new Vector3f( dir ) ) );
rv = mScene .mapViewToWorld( rv );
output .print( "light_source { -light_distance * " + printTuple3d( rv ) );
output .print( " " );
printColor( color );
output .println( " * multiplier_light_" + (i+1) + " }" );
Expand Down Expand Up @@ -216,7 +196,7 @@ String nameColor( Color color )
return "color_" + color .toString() .replace( ',', '_' );
}

private String printTuple3d( Tuple3f t )
private String printTuple3d( RealVector t )
{
StringBuilder buf = new StringBuilder( "<" );
buf .append( FORMAT.format(t.x) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import com.vzome.core.algebra.AlgebraicField;
import com.vzome.core.algebra.AlgebraicVector;
import com.vzome.core.editor.DocumentModel;
import com.vzome.core.editor.api.Selection;
import com.vzome.core.generic.ArrayComparator;
import com.vzome.core.model.Connector;
Expand All @@ -25,7 +24,7 @@ public class PartGeometryExporter extends VefExporter
{
private transient Selection selection;

public void exportDocument( DocumentModel doc, File file, Writer writer, int height, int width ) throws Exception
public void exportDocument( DocumentIntf doc, File file, Writer writer, int height, int width ) throws Exception
{
mModel = doc .getRenderedModel();
this .selection = doc .getEditorModel() .getSelection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.vzome.core.editor.DocumentModel;
import com.vzome.xml.DomSerializer;

/**
Expand All @@ -22,7 +21,7 @@
public class SideEffectsExporter extends DocumentExporter
{
@Override
public void exportDocument( DocumentModel document, File file, Writer writer, int height, int width ) throws Exception
public void exportDocument( com.vzome.core.exporters.DocumentIntf document, File file, Writer writer, int height, int width ) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance();
factory .setNamespaceAware( true );
Expand Down
34 changes: 33 additions & 1 deletion core/src/main/java/com/vzome/core/viewing/Camera.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.vzome.core.math.RealVector;
import com.vzome.xml.DomUtils;

public class Camera
public class Camera implements CameraIntf
{
/**
* The original frustum.
Expand Down Expand Up @@ -384,4 +384,36 @@ public Camera( Element viewElem )
mLookDirection = new Vector3f( x, y, z );
}
}

@Override
public RealVector mapViewToWorld( RealVector rv )
{
Vector3f vector = new Vector3f( rv.x, rv.y, rv.z );
Matrix4f viewTrans = new Matrix4f();
this .getViewTransform( viewTrans );
viewTrans .invert();
viewTrans .transform( vector );
return new RealVector( vector.x, vector.y, vector.z );
}

@Override
public RealVector getLookAtPointRV()
{
Point3f vector = getLookAtPoint();
return new RealVector( vector.x, vector.y, vector.z );
}

@Override
public RealVector getLookDirectionRV()
{
Vector3f vector = getLookDirection();
return new RealVector( vector.x, vector.y, vector.z );
}

@Override
public RealVector getUpDirectionRV()
{
Vector3f vector = getUpDirection();
return new RealVector( vector.x, vector.y, vector.z );
}
}
22 changes: 22 additions & 0 deletions core/src/main/java/com/vzome/core/viewing/CameraIntf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.vzome.core.viewing;

import com.vzome.core.math.RealVector;

public interface CameraIntf
{
boolean isPerspective();

float getFieldOfView();

float getViewDistance();

float getMagnification();

RealVector getLookAtPointRV();

RealVector getLookDirectionRV();

RealVector getUpDirectionRV();

RealVector mapViewToWorld( RealVector rv );
}
3 changes: 3 additions & 0 deletions online/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ cicd/online.bash quickstart

## Legacy Code Dev Workflow

This workflow is no longer viable unless you *already* have all of the JSweet components built and installed locally.
The JSweet Artifactory server is offline for good, and so the build is not working.

You'll need [Maven](https://maven.apache.org/) installed for these steps.
You will also require an installation of [JDK 11](https://www.oracle.com/java/technologies/javase/jdk11-archive-downloads.html).

Expand Down
Loading

0 comments on commit 43bdc5e

Please sign in to comment.