Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 29 additions & 42 deletions mathics/builtin/box/graphics3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,40 +63,40 @@ def _prepare_elements(self, leaves, options, max_width=None):

if lighting == "System`Automatic":
self.lighting = [
{"type": "Ambient", "color": [0.3, 0.2, 0.4]},
{"type": "ambient", "color": [0.3, 0.2, 0.4]},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this handled by the transformation layer in mathics-graphics3d-backend ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but is weird to have primitive names lower-case and light names capital-case.

Copy link
Member

@rocky rocky Jul 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't saying this is wrong - just that mathics-threejs-backend also needs to be able to handle things like this.

The current plan is to release Mathics Django as is. And then after that settles we will have a slew of API breaking changes.

In general things, have breaking way too much recently. We need more control everywhere on limiting breakage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so just remove the viewpoint scaling by now as it isn't a breaking change?

Copy link
Member

@rocky rocky Jul 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With all of the changes it is hard to follow this PR. It feels like after things settle down we should plan on a major Graphics3D in compact JSON design.

Personally, I think that all of the design work and implementation in mathics-threejs-backend could go on now if you are so disposed. Nothing prevents creating whatever API is best and converting what we have now to that.

In the PRs here in the short term we should separate breaking API from non-breaking.

Adding fields to existing JSON is fine that isn't going to break older Mathics-Django that happen use Mathics inore dev. changing existing values would. And having mathics-threejs-backend ignore existing fields is fine too.

{
"type": "Directional",
"type": "directional",
"color": [0.8, 0.0, 0.0],
"position": [2, 0, 2],
"coords": [[2, 0, 2]],
},
{
"type": "Directional",
"type": "directional",
"color": [0.0, 0.8, 0.0],
"position": [2, 2, 2],
"coords": [[2, 2, 2]],
},
{
"type": "Directional",
"type": "directional",
"color": [0.0, 0.0, 0.8],
"position": [0, 2, 2],
"coords": [[0, 2, 2]],
},
]
elif lighting == "Neutral": # Lighting->"Neutral"
self.lighting = [
{"type": "Ambient", "color": [0.3, 0.3, 0.3]},
{"type": "ambient", "color": [0.3, 0.3, 0.3]},
{
"type": "Directional",
"type": "directional",
"color": [0.3, 0.3, 0.3],
"position": [2, 0, 2],
"coords": [[2, 0, 2]],
},
{
"type": "Directional",
"type": "directional",
"color": [0.3, 0.3, 0.3],
"position": [2, 2, 2],
"coords": [[2, 2, 2]],
},
{
"type": "Directional",
"type": "directional",
"color": [0.3, 0.3, 0.3],
"position": [0, 2, 2],
"coords": [[0, 2, 2]],
},
]
elif lighting == "System`None":
Expand All @@ -114,7 +114,7 @@ def _prepare_elements(self, leaves, options, max_width=None):
color = get_class(head)(light[1])
if light[0] == '"Ambient"':
self.lighting.append(
{"type": "Ambient", "color": color.to_rgba()}
{"type": "ambient", "color": color.to_rgba()}
)
elif light[0] == '"Directional"':
position = [0, 0, 0]
Expand All @@ -129,9 +129,9 @@ def _prepare_elements(self, leaves, options, max_width=None):
]
self.lighting.append(
{
"type": "Directional",
"type": "directional",
"color": color.to_rgba(),
"position": position,
"coords": [position],
}
)
elif light[0] == '"Point"':
Expand All @@ -140,9 +140,9 @@ def _prepare_elements(self, leaves, options, max_width=None):
position = light[2]
self.lighting.append(
{
"type": "Point",
"type": "point",
"color": color.to_rgba(),
"position": position,
"coords": [position],
}
)
elif light[0] == '"Spot"':
Expand All @@ -165,9 +165,9 @@ def _prepare_elements(self, leaves, options, max_width=None):
angle = light[3]
self.lighting.append(
{
"type": "Spot",
"type": "spot",
"color": color.to_rgba(),
"position": position,
"coords": [position],
"target": target,
"angle": angle,
}
Expand Down Expand Up @@ -295,20 +295,14 @@ def calc_dimensions(final_pass=True):
# Rescale lighting
for i, light in enumerate(self.lighting):
if self.lighting[i]["type"] != "Ambient":
self.lighting[i]["position"] = [
light["position"][j] * boxscale[j] for j in range(3)
self.lighting[i]["coords"][0] = [
light["coords"][0][j] * boxscale[j] for j in range(3)
]
if self.lighting[i]["type"] == "Spot":
self.lighting[i]["target"] = [
light["target"][j] * boxscale[j] for j in range(3)
]

# Rescale viewpoint
self.viewpoint = [
vp * max([xmax - xmin, ymax - ymin, zmax - zmin])
for vp in self.viewpoint
]

w = 0 if (xmin is None or xmax is None) else xmax - xmin
h = 0 if (ymin is None or ymax is None) else ymax - ymin

Expand Down Expand Up @@ -363,17 +357,14 @@ def boxes_to_json(self, leaves=None, **options):

elements._apply_boxscaling(boxscale)

xmin, xmax, ymin, ymax, zmin, zmax, boxscale, w, h = calc_dimensions()
elements.view_width = w

# FIXME: json is the only thing we can convert MathML into.
# Handle other graphics formats.
format_fn = lookup_method(elements, "json")

json_repr = format_fn(elements, **options)

# TODO: Cubeoid (like this)
# json_repr = [{'faceColor': (1, 1, 1, 1), 'position': [(0,0,0), None],
# json_repr = [{'color': (1, 1, 1, 1), 'position': [(0,0,0), None],
# 'size':[(1,1,1), None], 'type': 'cube'}]

json_repr = json.dumps(
Expand All @@ -384,14 +375,6 @@ def boxes_to_json(self, leaves=None, **options):
"ticks": ticks,
"ticks_style": js_ticks_style,
},
"extent": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just ignore it in the other side?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but why keep it there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. It will change and should. But not before the Mathics-Django release.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good way to approach this is to start going over the API doc in mathics-threejs-backend review that and then come back here to make it match that specification.

But all of this is major API upheaval. I'd like to have at least one more stable release before that.

"xmin": xmin,
"xmax": xmax,
"ymin": ymin,
"ymax": ymax,
"zmin": zmin,
"zmax": zmax,
},
"lighting": self.lighting,
"viewpoint": self.viewpoint,
}
Expand Down Expand Up @@ -601,7 +584,11 @@ def boxes_to_tex(self, leaves=None, **options):
""".format(
asy_number(width / 60),
asy_number(height / 60),
self.viewpoint,
# Rescale viewpoint
[
vp * max([xmax - xmin, ymax - ymin, zmax - zmin])
for vp in self.viewpoint
],
asy,
boundbox_asy,
)
Expand Down
7 changes: 3 additions & 4 deletions mathics/format/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def convert_coord_collection(
"type": object_type,
"coords": [coords.pos() for coords in items],
"opacity": opacity,
"rgb_color": color[:3],
},
}
)
Expand Down Expand Up @@ -93,7 +92,7 @@ def cylinder_3d_box(self):
[self.points],
"cylinder",
face_color,
{"faceColor": face_color, "radius": self.radius},
{"color": face_color, "radius": self.radius},
)
# print("### json Cylinder3DBox", data)
return data
Expand Down Expand Up @@ -163,7 +162,7 @@ def polygon_3d_box(self) -> list:
self.lines,
"polygon",
face_color,
{"faceColor": face_color},
{"color": face_color},
)
# print("### json Polygon3DBox", data)
return data
Expand All @@ -180,7 +179,7 @@ def sphere_3d_box(self) -> list:
[self.points],
"sphere",
face_color,
{"faceColor": face_color, "radius": self.radius},
{"color": face_color, "radius": self.radius},
)
# print("### json Sphere3DBox", data)
return data
Expand Down