Skip to main content

oxmpl_js/base/
problem_definition.rs

1// Copyright (c) 2025 Ross Gardiner, Junior Sundar
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5use crate::base::{
6    compound_state::JsCompoundState, compound_state_space::JsCompoundStateSpace, goal::JsGoal,
7    real_vector_state::JsRealVectorState, real_vector_state_space::JsRealVectorStateSpace,
8    se2_state::JsSE2State, se2_state_space::JsSE2StateSpace, se3_state::JsSE3State,
9    se3_state_space::JsSE3StateSpace, so2_state::JsSO2State, so2_state_space::JsSO2StateSpace,
10    so3_state::JsSO3State, so3_state_space::JsSO3StateSpace,
11};
12use oxmpl::base::{
13    problem_definition::ProblemDefinition,
14    space::{
15        CompoundStateSpace, RealVectorStateSpace, SE2StateSpace, SE3StateSpace, SO2StateSpace,
16        SO3StateSpace,
17    },
18    state::{CompoundState, RealVectorState, SE2State, SE3State, SO2State, SO3State},
19};
20use std::sync::Arc;
21use wasm_bindgen::prelude::*;
22
23pub enum ProblemDefinitionVariant {
24    RealVector(Arc<ProblemDefinition<RealVectorState, RealVectorStateSpace, JsGoal>>),
25    SO2(Arc<ProblemDefinition<SO2State, SO2StateSpace, JsGoal>>),
26    SO3(Arc<ProblemDefinition<SO3State, SO3StateSpace, JsGoal>>),
27    SE2(Arc<ProblemDefinition<SE2State, SE2StateSpace, JsGoal>>),
28    SE3(Arc<ProblemDefinition<SE3State, SE3StateSpace, JsGoal>>),
29    Compound(Arc<ProblemDefinition<CompoundState, CompoundStateSpace, JsGoal>>),
30}
31
32#[wasm_bindgen(js_name = ProblemDefinition)]
33pub struct JsProblemDefinition {
34    pub(crate) inner: ProblemDefinitionVariant,
35}
36
37#[wasm_bindgen(js_class = ProblemDefinition)]
38impl JsProblemDefinition {
39    #[wasm_bindgen(js_name = fromRealVectorState)]
40    pub fn from_real_vector_state(
41        space: &JsRealVectorStateSpace,
42        start: &JsRealVectorState,
43        goal: &JsGoal,
44    ) -> Self {
45        let pd = ProblemDefinition {
46            space: Arc::new(space.inner.lock().unwrap().clone()),
47            start_states: vec![(*start.inner).clone()],
48            goal: Arc::new(goal.clone()),
49        };
50        Self {
51            inner: ProblemDefinitionVariant::RealVector(Arc::new(pd)),
52        }
53    }
54
55    #[wasm_bindgen(js_name = fromSO2State)]
56    pub fn from_so2_state(space: &JsSO2StateSpace, start: &JsSO2State, goal: &JsGoal) -> Self {
57        let pd = ProblemDefinition {
58            space: Arc::new(space.inner.lock().unwrap().clone()),
59            start_states: vec![(*start.inner).clone()],
60            goal: Arc::new(goal.clone()),
61        };
62        Self {
63            inner: ProblemDefinitionVariant::SO2(Arc::new(pd)),
64        }
65    }
66
67    #[wasm_bindgen(js_name = fromSO3State)]
68    pub fn from_so3_state(space: &JsSO3StateSpace, start: &JsSO3State, goal: &JsGoal) -> Self {
69        let pd = ProblemDefinition {
70            space: Arc::new(space.inner.lock().unwrap().clone()),
71            start_states: vec![(*start.inner).clone()],
72            goal: Arc::new(goal.clone()),
73        };
74        Self {
75            inner: ProblemDefinitionVariant::SO3(Arc::new(pd)),
76        }
77    }
78
79    #[wasm_bindgen(js_name = fromSE2State)]
80    pub fn from_se2_state(space: &JsSE2StateSpace, start: &JsSE2State, goal: &JsGoal) -> Self {
81        let pd = ProblemDefinition {
82            space: Arc::new(space.inner.lock().unwrap().clone()),
83            start_states: vec![(*start.inner).clone()],
84            goal: Arc::new(goal.clone()),
85        };
86        Self {
87            inner: ProblemDefinitionVariant::SE2(Arc::new(pd)),
88        }
89    }
90
91    #[wasm_bindgen(js_name = fromSE3State)]
92    pub fn from_se3_state(space: &JsSE3StateSpace, start: &JsSE3State, goal: &JsGoal) -> Self {
93        let pd = ProblemDefinition {
94            space: Arc::new(space.inner.lock().unwrap().clone()),
95            start_states: vec![(*start.inner).clone()],
96            goal: Arc::new(goal.clone()),
97        };
98        Self {
99            inner: ProblemDefinitionVariant::SE3(Arc::new(pd)),
100        }
101    }
102
103    #[wasm_bindgen(js_name = fromCompoundState)]
104    pub fn from_compound_state(
105        space: &JsCompoundStateSpace,
106        start: &JsCompoundState,
107        goal: &JsGoal,
108    ) -> Self {
109        let pd = ProblemDefinition {
110            space: Arc::new(space.inner.lock().unwrap().clone()),
111            start_states: vec![(*start.inner).clone()],
112            goal: Arc::new(goal.clone()),
113        };
114        Self {
115            inner: ProblemDefinitionVariant::Compound(Arc::new(pd)),
116        }
117    }
118}