Skip to content

Commit

Permalink
🛠️(CommonVecOp): add push and pop transformation commands
Browse files Browse the repository at this point in the history
Added `CommonOpPushTrans` and `CommonOpPopTrans` to manage a stack of local transformations. These commands allow pushing a new 3x3 transformation matrix onto the stack and popping the most recent one, respectively. The `GenerateCtx` struct now includes a `local_trans` field to store these matrices.

Signed-off-by: Benign X <[email protected]>
  • Loading branch information
W-Mai committed Nov 12, 2024
1 parent ec290db commit 18ae4f7
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/common_vec_op/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl IVisData for VecLineData {
struct GenerateCtx {
grouping: bool,
cursor: PlotPoint,
local_trans: Vec<[[f64; 3]; 3]>,
}

#[derive(Debug, Clone)]
Expand All @@ -81,6 +82,7 @@ impl IVisDataGenerator for VecLineGen {
let mut gen_ctx = AnyData::new(GenerateCtx {
grouping: false,
cursor: PlotPoint::new(0.0, 0.0),
local_trans: vec![],
});
let mut points_total = vec![];
let mut points: Vec<Box<dyn IVisData>> = vec![];
Expand Down Expand Up @@ -148,6 +150,10 @@ struct CommonOpCUBI;

struct CommonOpEND;

struct CommonOpPushTrans;

struct CommonOpPopTrans;

impl ICommandDescription for CommonOpMOVE {
fn name(&self) -> Vec<&str> {
["MOVE"].into()
Expand Down Expand Up @@ -273,6 +279,60 @@ impl ICommandDescription for CommonOpCUBI {
}
}

impl ICommandDescription for CommonOpPushTrans {
fn name(&self) -> Vec<&str> {
["PUSH_TRANS"].into()
}

fn argc(&self) -> usize {
9
}

fn operate(&self, ctx: &mut AnyData, argv: Rc<Vec<AnyData>>) -> Vec<AnyData> {
let ctx = ctx.cast_mut::<GenerateCtx>();
let trans_matrix: [[f64; 3]; 3] = [
[
*argv[0].cast_ref(),
*argv[1].cast_ref(),
*argv[2].cast_ref(),
],
[
*argv[3].cast_ref(),
*argv[4].cast_ref(),
*argv[5].cast_ref(),
],
[
*argv[6].cast_ref(),
*argv[7].cast_ref(),
*argv[8].cast_ref(),
],
];

ctx.local_trans.push(trans_matrix);

vec![]
}
}

impl ICommandDescription for CommonOpPopTrans {
fn name(&self) -> Vec<&str> {
["POP_TRANS"].into()
}

fn argc(&self) -> usize {
0
}

fn operate(&self, ctx: &mut AnyData, _argv: Rc<Vec<AnyData>>) -> Vec<AnyData> {
let ctx = ctx.cast_mut::<GenerateCtx>();
if ctx.local_trans.is_empty() {
return vec![];
}
ctx.local_trans.pop();
vec![]
}
}

impl ICommandDescription for CommonOpEND {
fn name(&self) -> Vec<&str> {
["END", "CLOSE"].into()
Expand All @@ -299,6 +359,8 @@ impl ICommandSyntax for CommonVecOpSyntax {
&CommonOpQUAD {},
&CommonOpCUBI {},
&CommonOpEND {},
&CommonOpPushTrans {},
&CommonOpPopTrans {},
]
}
}

0 comments on commit 18ae4f7

Please sign in to comment.