Skip to content

Commit

Permalink
Merge pull request #38 from jonmmease/jonmmease/group_offset_clip
Browse files Browse the repository at this point in the history
Handle group origin calculations in shader logic
  • Loading branch information
jonmmease authored Jan 27, 2024
2 parents b42928d + 1681132 commit c1542ef
Show file tree
Hide file tree
Showing 29 changed files with 384 additions and 155 deletions.
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions sg2d-vega/src/marks/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 13 additions & 14 deletions sg2d-vega/src/marks/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions sg2d-vega/src/marks/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
)),
)
}
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions sg2d-vega/src/marks/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions sg2d-vega/src/marks/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
))
}

Expand All @@ -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 {
Expand Down
15 changes: 5 additions & 10 deletions sg2d-vega/src/marks/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions sg2d-vega/src/marks/trail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion sg2d-vega/src/scene_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c1542ef

Please sign in to comment.