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

auto_color class-ids if they are present #1783

Merged
merged 4 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
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
61 changes: 39 additions & 22 deletions crates/re_viewer/src/ui/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,51 @@ pub struct Annotations {

impl Annotations {
pub fn class_description(&self, class_id: Option<ClassId>) -> ResolvedClassDescription<'_> {
ResolvedClassDescription(
class_id.and_then(|class_id| self.context.class_map.get(&class_id)),
)
ResolvedClassDescription {
class_id,
class_description: class_id.and_then(|class_id| self.context.class_map.get(&class_id)),
}
}
}

pub struct ResolvedClassDescription<'a>(pub Option<&'a ClassDescription>);
pub struct ResolvedClassDescription<'a> {
pub class_id: Option<ClassId>,
pub class_description: Option<&'a ClassDescription>,
}

impl<'a> ResolvedClassDescription<'a> {
pub fn annotation_info(&self) -> ResolvedAnnotationInfo {
ResolvedAnnotationInfo(self.0.map(|desc| desc.info.clone()))
ResolvedAnnotationInfo {
class_id: self.class_id,
annotation_info: self.class_description.map(|desc| desc.info.clone()),
}
}

/// Merges class annotation info with keypoint annotation info (if existing respectively).
pub fn annotation_info_with_keypoint(&self, keypoint_id: KeypointId) -> ResolvedAnnotationInfo {
if let Some(desc) = self.0 {
if let Some(desc) = self.class_description {
// Assuming that keypoint annotation is the rarer case, merging the entire annotation ahead of time
// is cheaper than doing it lazily (which would cause more branches down the line for callsites without keypoints)
if let Some(keypoint_annotation_info) = desc.keypoint_map.get(&keypoint_id) {
ResolvedAnnotationInfo(Some(AnnotationInfo {
id: keypoint_id.0,
label: keypoint_annotation_info
.label
.clone()
.or_else(|| desc.info.label.clone()),
color: keypoint_annotation_info.color.or(desc.info.color),
}))
ResolvedAnnotationInfo {
class_id: self.class_id,
annotation_info: Some(AnnotationInfo {
id: keypoint_id.0,
label: keypoint_annotation_info
.label
.clone()
.or_else(|| desc.info.label.clone()),
color: keypoint_annotation_info.color.or(desc.info.color),
}),
}
} else {
self.annotation_info()
}
} else {
ResolvedAnnotationInfo(None)
ResolvedAnnotationInfo {
class_id: self.class_id,
annotation_info: None,
}
}
}
}
Expand All @@ -66,7 +79,10 @@ pub enum DefaultColor<'a> {
}

#[derive(Clone)]
pub struct ResolvedAnnotationInfo(pub Option<AnnotationInfo>);
pub struct ResolvedAnnotationInfo {
pub class_id: Option<ClassId>,
pub annotation_info: Option<AnnotationInfo>,
}

impl ResolvedAnnotationInfo {
pub fn color(
Expand All @@ -76,17 +92,18 @@ impl ResolvedAnnotationInfo {
) -> re_renderer::Color32 {
if let Some([r, g, b, a]) = color {
re_renderer::Color32::from_rgba_premultiplied(*r, *g, *b, *a)
} else if let Some(color) = self.0.as_ref().and_then(|info| {
} else if let Some(color) = self.annotation_info.as_ref().and_then(|info| {
info.color
.map(|c| c.into())
.or_else(|| Some(auto_color(info.id)))
}) {
color
} else {
match default_color {
DefaultColor::TransparentBlack => re_renderer::Color32::TRANSPARENT,
DefaultColor::OpaqueWhite => re_renderer::Color32::WHITE,
DefaultColor::EntityPath(entity_path) => {
match (self.class_id, default_color) {
(Some(class_id), _) if class_id.0 != 0 => auto_color(class_id.0),
(_, DefaultColor::TransparentBlack) => re_renderer::Color32::TRANSPARENT,
(_, DefaultColor::OpaqueWhite) => re_renderer::Color32::WHITE,
(_, DefaultColor::EntityPath(entity_path)) => {
auto_color((entity_path.hash64() % std::u16::MAX as u64) as u16)
}
}
Expand All @@ -97,7 +114,7 @@ impl ResolvedAnnotationInfo {
if let Some(label) = label {
Some(label.clone())
} else {
self.0
self.annotation_info
.as_ref()
.and_then(|info| info.label.as_ref().map(|label| label.0.clone()))
}
Expand Down
2 changes: 1 addition & 1 deletion examples/python/api_demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def run_segmentation() -> None:
class_ids=np.array([42], dtype=np.uint8),
)

rr.log_text_entry("logs/seg_demo_log", "no rects, default colored points, a single point has a label")
rr.log_text_entry("logs/seg_demo_log", "default colored rects, default colored points, a single point has a label")

# Log an initial segmentation map with arbitrary colors
rr.set_time_seconds("sim_time", 2)
Expand Down