Skip to content

Commit

Permalink
Merge pull request #17 from jonmmease/jonmmease/zindex
Browse files Browse the repository at this point in the history
Support zindex encoding channel for ordering mark items
  • Loading branch information
jonmmease authored Jan 11, 2024
2 parents 3ee0209 + 287792f commit f72f6d1
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"width": 115,
"height": 100,
"origin_x": 15,
"origin_y": 0
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions sg2d-vega-test-data/vega-scenegraphs/symbol/zindex_circles.sg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"marktype": "group",
"name": "root",
"role": "frame",
"interactive": true,
"clip": false,
"items": [
{
"items": [
{
"marktype": "symbol",
"role": "mark",
"interactive": true,
"clip": false,
"items": [
{
"zindex": 3,
"x": 30,
"y": 40,
"fill": "red",
"stroke": "black",
"strokeWidth": 3,
"size": 600,
"shape": "circle",
"angle": 0
},
{
"zindex": 1,
"x": 30,
"y": 40,
"fill": "blue",
"stroke": "black",
"strokeWidth": 3,
"size": 1200,
"shape": "circle",
"angle": 30
},
{
"zindex": 0,
"x": 10,
"y": 40,
"fill": "green",
"stroke": "black",
"strokeWidth": 3,
"size": 1800,
"shape": "circle",
"angle": 90
},
{
"zindex": 2,
"x": 50,
"y": 40,
"fill": "aqua",
"stroke": "black",
"strokeWidth": 3,
"size": 2200,
"shape": "circle",
"angle": 120
},
{
"zindex": 0,
"x": 30,
"y": 40,
"fill": "pink",
"stroke": "black",
"strokeWidth": 3,
"size": 2800,
"shape": "circle",
"angle": 180
}
],
"zindex": 0
}
],
"x": 0,
"y": 0,
"width": 100,
"height": 100
}
],
"zindex": 0
}
36 changes: 36 additions & 0 deletions sg2d-vega-test-data/vega-specs/symbol/zindex_circles.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"width": 100,
"height": 100,
"background": "white",
"data": [
{
"name": "data_1",
"values": [
{"x": 30, "z": 3, "angle": 0, "c": "red", "size": 600},
{"x": 30, "z": 1, "angle": 30, "c": "blue", "size": 1200},
{"x": 10, "z": 0, "angle": 90, "c": "green", "size": 1800},
{"x": 50, "z": 2, "angle": 120, "c": "aqua", "size": 2200},
{"x": 30, "z": 0, "angle": 180, "c": "pink", "size": 2800}
]
}
],
"marks": [
{
"type": "symbol",
"from": {"data": "data_1"},
"encode": {
"update": {
"x": {"field": "x"},
"y": {"value": 40},
"zindex": {"field": "z"},
"shape": {"value": "circle"},
"fill": {"field": "c"},
"stroke": {"value": "black"},
"strokeWidth": {"value": 3},
"size": {"field": "size"},
"angle": {"field": "angle"}
}
}
}
]
}
10 changes: 10 additions & 0 deletions sg2d-vega/src/marks/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct VegaRectItem {
pub y2: Option<f32>,
pub fill: Option<String>,
pub fill_opacity: Option<f32>,
pub zindex: Option<i32>,
}

impl VegaMarkItem for VegaRectItem {}
Expand All @@ -37,6 +38,7 @@ impl VegaMarkContainer<VegaRectItem> {
let mut width = Vec::<f32>::new();
let mut height = Vec::<f32>::new();
let mut fill = Vec::<[f32; 3]>::new();
let mut zindex = Vec::<i32>::new();

// For each item, append explicit values to corresponding vector
for item in &self.items {
Expand All @@ -52,6 +54,9 @@ impl VegaMarkContainer<VegaRectItem> {
let c = csscolorparser::parse(v)?;
fill.push([c.r as f32, c.g as f32, c.b as f32])
}
if let Some(v) = item.zindex {
zindex.push(v);
}
}

// Override values with vectors
Expand All @@ -73,6 +78,11 @@ impl VegaMarkContainer<VegaRectItem> {
if fill.len() == len {
mark.fill = EncodingValue::Array { values: fill };
}
if zindex.len() == len {
let mut indices: Vec<usize> = (0..len).collect();
indices.sort_by_key(|i| zindex[*i]);
mark.indices = Some(indices);
}

Ok(SceneMark::Rect(mark))
}
Expand Down
11 changes: 11 additions & 0 deletions sg2d-vega/src/marks/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct VegaRuleItem {
pub stroke: Option<String>,
pub stroke_width: Option<f32>,
pub stroke_cap: Option<StrokeCap>,
pub zindex: Option<i32>,
}

impl VegaMarkItem for VegaRuleItem {}
Expand All @@ -38,6 +39,7 @@ impl VegaMarkContainer<VegaRuleItem> {
let mut stroke = Vec::<[f32; 3]>::new();
let mut stroke_width = Vec::<f32>::new();
let mut stroke_cap = Vec::<StrokeCap>::new();
let mut zindex = Vec::<i32>::new();

// For each item, append explicit values to corresponding vector
for item in &self.items {
Expand All @@ -58,6 +60,10 @@ impl VegaMarkContainer<VegaRuleItem> {
if let Some(s) = item.stroke_cap {
stroke_cap.push(s);
}

if let Some(v) = item.zindex {
zindex.push(v);
}
}

// Override values with vectors
Expand Down Expand Up @@ -87,6 +93,11 @@ impl VegaMarkContainer<VegaRuleItem> {
if stroke_cap.len() == len {
mark.stroke_cap = EncodingValue::Array { values: stroke_cap };
}
if zindex.len() == len {
let mut indices: Vec<usize> = (0..len).collect();
indices.sort_by_key(|i| zindex[*i]);
mark.indices = Some(indices);
}

Ok(SceneMark::Rule(mark))
}
Expand Down
10 changes: 10 additions & 0 deletions sg2d-vega/src/marks/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct VegaSymbolItem {
pub stroke_width: Option<f32>,
pub stroke_opacity: Option<f32>,
pub angle: Option<f32>,
pub zindex: Option<i32>,
}

impl VegaMarkItem for VegaSymbolItem {}
Expand Down Expand Up @@ -67,6 +68,7 @@ impl VegaMarkContainer<VegaSymbolItem> {
let mut stroke = Vec::<[f32; 4]>::new();
let mut stroke_width = Vec::<f32>::new();
let mut angle = Vec::<f32>::new();
let mut zindex = Vec::<i32>::new();

// For each item, append explicit values to corresponding vector
for item in &self.items {
Expand Down Expand Up @@ -99,6 +101,9 @@ impl VegaMarkContainer<VegaSymbolItem> {
if let Some(v) = item.angle {
angle.push(v);
}
if let Some(v) = item.zindex {
zindex.push(v);
}
}

// Override values with vectors
Expand All @@ -123,6 +128,11 @@ impl VegaMarkContainer<VegaSymbolItem> {
if angle.len() == len {
mark.angle = EncodingValue::Array { values: angle };
}
if zindex.len() == len {
let mut indices: Vec<usize> = (0..len).collect();
indices.sort_by_key(|i| zindex[*i]);
mark.indices = Some(indices);
}

Ok(SceneMark::Symbol(mark))
}
Expand Down
11 changes: 11 additions & 0 deletions sg2d-vega/src/marks/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct VegaTextItem {
pub font_weight: Option<FontWeightSpec>,
pub font_style: Option<FontStyleSpec>,
pub limit: Option<f32>,
pub zindex: Option<i32>,
}

impl VegaMarkItem for VegaTextItem {}
Expand Down Expand Up @@ -57,6 +58,7 @@ impl VegaMarkContainer<VegaTextItem> {
let mut font_weight = Vec::<FontWeightSpec>::new();
let mut font_style = Vec::<FontStyleSpec>::new();
let mut limit = Vec::<f32>::new();
let mut zindex = Vec::<i32>::new();

for item in &self.items {
x.push(item.x + origin[0]);
Expand Down Expand Up @@ -111,6 +113,10 @@ impl VegaMarkContainer<VegaTextItem> {
if let Some(v) = item.limit {
limit.push(v);
}

if let Some(v) = item.zindex {
zindex.push(v);
}
}

// Override values with vectors
Expand Down Expand Up @@ -164,6 +170,11 @@ impl VegaMarkContainer<VegaTextItem> {
if limit.len() == len {
mark.limit = EncodingValue::Array { values: limit };
}
if zindex.len() == len {
let mut indices: Vec<usize> = (0..len).collect();
indices.sort_by_key(|i| zindex[*i]);
mark.indices = Some(indices);
}
Ok(SceneMark::Text(Box::new(mark)))
}
}
1 change: 1 addition & 0 deletions sg2d-wgpu/tests/test_image_baselines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod test_image_baselines {
case("symbol", "wind_vector", 0.0015),
case("symbol", "wedge_angle", 0.001),
case("symbol", "wedge_stroke_angle", 0.001),
case("symbol", "zindex_circles", 0.001),
case("rule", "wide_rule_axes", 0.0001),
case("text", "bar_axis_labels", 0.025)
)]
Expand Down
13 changes: 8 additions & 5 deletions sg2d/src/marks/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@ pub struct RectMark {
pub width: EncodingValue<f32>,
pub height: EncodingValue<f32>,
pub fill: EncodingValue<[f32; 3]>,
pub indices: Option<Vec<usize>>,
}

impl RectMark {
pub fn x_iter(&self) -> Box<dyn Iterator<Item = &f32> + '_> {
self.x.as_iter(self.len as usize)
self.x.as_iter(self.len as usize, self.indices.as_ref())
}

pub fn y_iter(&self) -> Box<dyn Iterator<Item = &f32> + '_> {
self.y.as_iter(self.len as usize)
self.y.as_iter(self.len as usize, self.indices.as_ref())
}

pub fn width_iter(&self) -> Box<dyn Iterator<Item = &f32> + '_> {
self.width.as_iter(self.len as usize)
self.width.as_iter(self.len as usize, self.indices.as_ref())
}

pub fn height_iter(&self) -> Box<dyn Iterator<Item = &f32> + '_> {
self.height.as_iter(self.len as usize)
self.height
.as_iter(self.len as usize, self.indices.as_ref())
}

pub fn fill_iter(&self) -> Box<dyn Iterator<Item = &[f32; 3]> + '_> {
self.fill.as_iter(self.len as usize)
self.fill.as_iter(self.len as usize, self.indices.as_ref())
}
}

Expand All @@ -49,6 +51,7 @@ impl Default for RectMark {
fill: EncodingValue::Scalar {
value: [0.0, 0.0, 0.0],
},
indices: None,
}
}
}
19 changes: 12 additions & 7 deletions sg2d/src/marks/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,33 @@ pub struct RuleMark {
pub stroke: EncodingValue<[f32; 3]>,
pub stroke_width: EncodingValue<f32>,
pub stroke_cap: EncodingValue<StrokeCap>,
pub indices: Option<Vec<usize>>,
}

impl RuleMark {
pub fn x0_iter(&self) -> Box<dyn Iterator<Item = &f32> + '_> {
self.x0.as_iter(self.len as usize)
self.x0.as_iter(self.len as usize, self.indices.as_ref())
}
pub fn y0_iter(&self) -> Box<dyn Iterator<Item = &f32> + '_> {
self.y0.as_iter(self.len as usize)
self.y0.as_iter(self.len as usize, self.indices.as_ref())
}
pub fn x1_iter(&self) -> Box<dyn Iterator<Item = &f32> + '_> {
self.x1.as_iter(self.len as usize)
self.x1.as_iter(self.len as usize, self.indices.as_ref())
}
pub fn y1_iter(&self) -> Box<dyn Iterator<Item = &f32> + '_> {
self.y1.as_iter(self.len as usize)
self.y1.as_iter(self.len as usize, self.indices.as_ref())
}
pub fn stroke_iter(&self) -> Box<dyn Iterator<Item = &[f32; 3]> + '_> {
self.stroke.as_iter(self.len as usize)
self.stroke
.as_iter(self.len as usize, self.indices.as_ref())
}
pub fn stroke_width_iter(&self) -> Box<dyn Iterator<Item = &f32> + '_> {
self.stroke_width.as_iter(self.len as usize)
self.stroke_width
.as_iter(self.len as usize, self.indices.as_ref())
}
pub fn stroke_cap_iter(&self) -> Box<dyn Iterator<Item = &StrokeCap> + '_> {
self.stroke_cap.as_iter(self.len as usize)
self.stroke_cap
.as_iter(self.len as usize, self.indices.as_ref())
}
}

Expand All @@ -57,6 +61,7 @@ impl Default for RuleMark {
stroke_cap: EncodingValue::Scalar {
value: StrokeCap::Butt,
},
indices: None,
}
}
}
Loading

0 comments on commit f72f6d1

Please sign in to comment.