Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle group origin calculations in shader logic #38

Merged
merged 1 commit into from
Jan 27, 2024
Merged
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
Handle group origin calculations in shader logic, not vega translatio…
…n logic
  • Loading branch information
jonmmease committed Jan 27, 2024
commit 1681132c179391b2b7b2ea3557476824a70757a9
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/arc.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ pub struct VegaArcItem {
impl VegaMarkItem for VegaArcItem {}

impl VegaMarkContainer<VegaArcItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
// Init mark with scalar defaults
let mut mark = ArcMark {
clip: self.clip,
@@ -56,8 +56,8 @@ impl VegaMarkContainer<VegaArcItem> {

// For each item, append explicit values to corresponding vector
for item in &self.items {
x.push(item.x + origin[0]);
y.push(item.y + origin[1]);
x.push(item.x);
y.push(item.y);

if let Some(s) = item.start_angle {
start_angle.push(s);
10 changes: 5 additions & 5 deletions sg2d-vega/src/marks/area.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ pub struct VegaAreaItem {
impl VegaMarkItem for VegaAreaItem {}

impl VegaMarkContainer<VegaAreaItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
// Get shape of first item and use that for all items for now
let first = self.items.first();
let stroke_cap = first.and_then(|item| item.stroke_cap).unwrap_or_default();
@@ -80,13 +80,13 @@ impl VegaMarkContainer<VegaAreaItem> {
let mut defined = Vec::<bool>::new();

for item in &self.items {
x.push(item.x + origin[0]);
y.push(item.y + origin[1]);
x.push(item.x);
y.push(item.y);
if let Some(v) = &item.x2 {
x2.push(*v + origin[0]);
x2.push(*v);
}
if let Some(v) = &item.y2 {
y2.push(*v + origin[1]);
y2.push(*v);
}
if let Some(v) = item.defined {
defined.push(v);
27 changes: 13 additions & 14 deletions sg2d-vega/src/marks/group.rs
Original file line number Diff line number Diff line change
@@ -18,48 +18,47 @@ pub struct VegaGroupItem {
impl VegaMarkItem for VegaGroupItem {}

impl VegaGroupItem {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneGroup, VegaSceneGraphError> {
let new_origin = [self.x + origin[0], self.y + origin[1]];
pub fn to_scene_graph(&self) -> Result<SceneGroup, VegaSceneGraphError> {
let mut marks: Vec<SceneMark> = Vec::new();
for item in &self.items {
let item_marks: Vec<_> = match item {
VegaMark::Group(group) => group
.items
.iter()
.map(|item| Ok(SceneMark::Group(item.to_scene_graph(new_origin)?)))
.map(|item| Ok(SceneMark::Group(item.to_scene_graph()?)))
.collect::<Result<Vec<_>, VegaSceneGraphError>>()?,
VegaMark::Rect(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Rule(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Symbol(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Text(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Arc(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Path(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Shape(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Line(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Area(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Trail(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
VegaMark::Image(mark) => {
vec![mark.to_scene_graph(new_origin)?]
vec![mark.to_scene_graph()?]
}
};
marks.extend(item_marks);
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/image.rs
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ fn default_true() -> bool {
impl VegaMarkItem for VegaImageItem {}

impl VegaMarkContainer<VegaImageItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
let name = self
.name
.clone()
@@ -58,8 +58,8 @@ impl VegaMarkContainer<VegaImageItem> {
let fetcher = make_image_fetcher()?;

for item in &self.items {
x.push(item.x + origin[0]);
y.push(item.y + origin[1]);
x.push(item.x);
y.push(item.y);
align.push(item.align);
baseline.push(item.baseline);

6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/line.rs
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ pub struct VegaLineItem {
impl VegaMarkItem for VegaLineItem {}

impl VegaMarkContainer<VegaLineItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
// Get shape of first item and use that for all items for now
let first = self.items.first();
let stroke_width = first.and_then(|item| item.stroke_width).unwrap_or(1.0);
@@ -65,8 +65,8 @@ impl VegaMarkContainer<VegaLineItem> {
let mut defined = Vec::<bool>::new();

for item in &self.items {
x.push(item.x + origin[0]);
y.push(item.y + origin[1]);
x.push(item.x);
y.push(item.y);
if let Some(v) = item.defined {
defined.push(v);
}
8 changes: 4 additions & 4 deletions sg2d-vega/src/marks/path.rs
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ pub struct VegaPathItem {
impl VegaMarkItem for VegaPathItem {}

impl VegaMarkContainer<VegaPathItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
// Get shape of first item and use that for all items for now
let first = self.items.first();
let first_has_stroke = first.map(|item| item.stroke.is_some()).unwrap_or(false);
@@ -94,8 +94,8 @@ impl VegaMarkContainer<VegaPathItem> {
PathTransform::scale(item.scale_x.unwrap_or(1.0), item.scale_y.unwrap_or(1.0))
.then_rotate(Angle::degrees(item.angle.unwrap_or(0.0)))
.then_translate(Vector2D::new(
item.x.unwrap_or(0.0) + origin[0],
item.y.unwrap_or(0.0) + origin[1],
item.x.unwrap_or(0.0),
item.y.unwrap_or(0.0),
)),
)
}
@@ -117,7 +117,7 @@ impl VegaMarkContainer<VegaPathItem> {
mark.transform = EncodingValue::Array { values: transform };
} else {
mark.transform = EncodingValue::Scalar {
value: PathTransform::translation(origin[0], origin[1]),
value: PathTransform::identity(),
}
}
if zindex.len() == len {
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/rect.rs
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ pub struct VegaRectItem {
impl VegaMarkItem for VegaRectItem {}

impl VegaMarkContainer<VegaRectItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
let mut mark = RectMark {
clip: self.clip,
..Default::default()
@@ -52,8 +52,8 @@ impl VegaMarkContainer<VegaRectItem> {

// For each item, append explicit values to corresponding vector
for item in &self.items {
x.push(item.x + origin[0]);
y.push(item.y + origin[1]);
x.push(item.x);
y.push(item.y);
if let Some(v) = item.width {
width.push(v);
}
10 changes: 5 additions & 5 deletions sg2d-vega/src/marks/rule.rs
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ pub struct VegaRuleItem {
impl VegaMarkItem for VegaRuleItem {}

impl VegaMarkContainer<VegaRuleItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
// Init mark with scalar defaults
let mut mark = RuleMark {
clip: self.clip,
@@ -49,10 +49,10 @@ impl VegaMarkContainer<VegaRuleItem> {

// For each item, append explicit values to corresponding vector
for item in &self.items {
x0.push(item.x + origin[0]);
y0.push(item.y + origin[1]);
x1.push(item.x2.unwrap_or(item.x) + origin[0]);
y1.push(item.y2.unwrap_or(item.y) + origin[1]);
x0.push(item.x);
y0.push(item.y);
x1.push(item.x2.unwrap_or(item.x));
y1.push(item.y2.unwrap_or(item.y));

if let Some(v) = &item.stroke {
let opacity = item.stroke_opacity.unwrap_or(1.0) * item.opacity.unwrap_or(1.0);
8 changes: 4 additions & 4 deletions sg2d-vega/src/marks/shape.rs
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ pub struct VegaShapeItem {
impl VegaMarkItem for VegaShapeItem {}

impl VegaMarkContainer<VegaShapeItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
// Get shape of first item and use that for all items for now
let first = self.items.first();
let first_has_stroke = first.map(|item| item.stroke.is_some()).unwrap_or(false);
@@ -79,8 +79,8 @@ impl VegaMarkContainer<VegaShapeItem> {
// Build transform
if item.x.is_some() || item.y.is_some() {
transform.push(PathTransform::translation(
item.x.unwrap_or(0.0) + origin[0],
item.y.unwrap_or(0.0) + origin[1],
item.x.unwrap_or(0.0),
item.y.unwrap_or(0.0),
))
}

@@ -101,7 +101,7 @@ impl VegaMarkContainer<VegaShapeItem> {
mark.transform = EncodingValue::Array { values: transform };
} else {
mark.transform = EncodingValue::Scalar {
value: PathTransform::translation(origin[0], origin[1]),
value: PathTransform::identity(),
}
}
if zindex.len() == len {
15 changes: 5 additions & 10 deletions sg2d-vega/src/marks/symbol.rs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ pub struct VegaSymbolItem {
impl VegaMarkItem for VegaSymbolItem {}

impl VegaMarkContainer<VegaSymbolItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
// Get shape of first item and use that for all items for now
let first = self.items.first();
let first_shape = first
@@ -64,14 +64,9 @@ impl VegaMarkContainer<VegaSymbolItem> {
clip: false,
len: 2,
x: EncodingValue::Array {
values: vec![
origin[0] + item.x - width / 2.0,
origin[0] + item.x + width / 2.0,
],
},
y: EncodingValue::Scalar {
value: origin[1] + item.y,
values: vec![item.x - width / 2.0, item.x + width / 2.0],
},
y: EncodingValue::Scalar { value: item.y },
stroke,
stroke_width: item.stroke_width.unwrap_or(1.0),
stroke_cap: item.stroke_cap.unwrap_or_default(),
@@ -132,8 +127,8 @@ impl VegaMarkContainer<VegaSymbolItem> {

// For each item, append explicit values to corresponding vector
for item in &self.items {
x.push(item.x + origin[0]);
y.push(item.y + origin[1]);
x.push(item.x);
y.push(item.y);

let base_opacity = item.opacity.unwrap_or(1.0);
if let Some(v) = &item.fill {
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/text.rs
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ pub struct VegaTextItem {
impl VegaMarkItem for VegaTextItem {}

impl VegaMarkContainer<VegaTextItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
// Init mark with scalar defaults
let mut mark = TextMark {
clip: self.clip,
@@ -61,8 +61,8 @@ impl VegaMarkContainer<VegaTextItem> {
let mut zindex = Vec::<i32>::new();

for item in &self.items {
x.push(item.x + origin[0]);
y.push(item.y + origin[1]);
x.push(item.x);
y.push(item.y);
text.push(item.text.clone());

if let Some(v) = item.align {
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/trail.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ pub struct VegaTrailItem {
impl VegaMarkItem for VegaTrailItem {}

impl VegaMarkContainer<VegaTrailItem> {
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> {
pub fn to_scene_graph(&self) -> Result<SceneMark, VegaSceneGraphError> {
// Get shape of first item and use that for all items for now
let first = self.items.first();
let mut gradients = Vec::<Gradient>::new();
@@ -50,8 +50,8 @@ impl VegaMarkContainer<VegaTrailItem> {
let mut defined = Vec::<bool>::new();

for item in &self.items {
x.push(item.x + origin[0]);
y.push(item.y + origin[1]);
x.push(item.x);
y.push(item.y);
if let Some(v) = item.size {
size.push(v);
}
3 changes: 2 additions & 1 deletion sg2d-vega/src/scene_graph.rs
Original file line number Diff line number Diff line change
@@ -18,13 +18,14 @@ impl VegaSceneGraph {
.scenegraph
.items
.iter()
.map(|group| group.to_scene_graph(self.origin))
.map(|group| group.to_scene_graph())
.collect::<Result<Vec<_>, VegaSceneGraphError>>()?;

Ok(SceneGraph {
groups,
width: self.width,
height: self.height,
origin: self.origin,
})
}
}
Loading
Loading