Skip to content

Commit 14573f1

Browse files
committed
Documentation for modules dim4, graphics & index
1 parent 666e019 commit 14573f1

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed

src/dim4.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
use std::fmt;
22
use std::ops::Index;
33

4+
/// Dim4 is used to store [Array](./struct.Array.html) dimensions
45
#[derive(Copy, Clone)]
56
pub struct Dim4 {
67
dims: [u64; 4],
78
}
89

10+
/// Default trait for Dim4 returns an Array of dimensions [1, 1, 1, 1]
911
impl Default for Dim4 {
1012
fn default() -> Dim4 {
1113
Dim4 { dims:[1, 1, 1, 1] }
1214
}
1315
}
1416

17+
/// Enables index operation for Dim4
18+
///
19+
/// # Examples
20+
///
21+
/// ```
22+
/// let dims = Dim4::new(&[4, 4, 2, 1]);
23+
/// println!("0th Dimension length is {}", dims[0]); // -> 4
24+
/// println!("1th Dimension length is {}", dims[1]); // -> 4
25+
/// println!("2th Dimension length is {}", dims[2]); // -> 2
26+
/// println!("3th Dimension length is {}", dims[3]); // -> 1
27+
/// ```
1528
impl Index<usize> for Dim4 {
1629
type Output = u64;
1730

@@ -20,21 +33,38 @@ impl Index<usize> for Dim4 {
2033
}
2134
}
2235

36+
/// Enables use of Dim4 objects for printing it to display
37+
///
38+
/// # Examples
39+
///
40+
/// ```
41+
/// let dims = Dim4::new(&[4, 4, 2, 1]);
42+
/// println!("0th Dimension length is {}", dims[0]); // -> 4
43+
/// ```
2344
impl fmt::Display for Dim4 {
2445
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2546
write!(f, "[{} {} {} {}]", self.dims[0], self.dims[1], self.dims[2], self.dims[3])
2647
}
2748
}
2849

2950
impl Dim4 {
51+
/// Create Dim4 object
52+
///
53+
/// # Examples
54+
///
55+
/// ```
56+
/// let dims = Dim4::new(&[4, 4, 2, 1]);
57+
/// ```
3058
pub fn new(dims: &[u64; 4]) -> Dim4 {
3159
Dim4 { dims: dims.clone(), }
3260
}
3361

62+
/// Get the number of elements represented by Dim4 object
3463
pub fn elements(&self) -> u64 {
3564
self.dims[0]*self.dims[1]*self.dims[2]*self.dims[3]
3665
}
3766

67+
/// Get the number of dimensions of Dim4
3868
pub fn ndims(&self) -> usize {
3969
let nelems = self.elements();
4070
match nelems {
@@ -49,6 +79,7 @@ impl Dim4 {
4979
}
5080
}
5181

82+
// Get the dimensions as a slice of 4 values
5283
pub fn get(&self) -> &[u64; 4] {
5384
&self.dims
5485
}

src/graphics.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ extern {
2626
minval: c_double, maxval: c_double, props: CellPtr) -> c_int;
2727
}
2828

29+
/// Represents a sub-view of Window
30+
///
31+
/// This struct is used in conjunction with [Window](./struct.Window.html) in multiview
32+
/// mode to render multiple targets to sub-regions of a given window.
33+
///
2934
#[repr(C)]
3035
pub struct Cell {
3136
pub row: i32,
@@ -34,6 +39,36 @@ pub struct Cell {
3439
pub cmap: ColorMap,
3540
}
3641

42+
/// Used to render [Array](./struct.Array.html) objects
43+
///
44+
/// The renderings can be either plots, histograms or simply just image displays.
45+
/// A single window can also display multiple of the above renderings at the same time, which
46+
/// is known as multiview mode. An example of that is given below.
47+
///
48+
/// # Examples
49+
///
50+
/// ```
51+
/// let wnd = Window::new(1280, 720, String::from("Image Histogram")).unwrap();
52+
/// let img = match load_image("Path to image", true/*If color image, 'false' otherwise*/) {
53+
/// Ok(img) => img,
54+
/// Err(err) => panic!("Image loading failed with error code {}", err),
55+
/// };
56+
/// let hst = histogram(img, 256, 0, 255).unwrap();
57+
///
58+
/// loop {
59+
/// wnd.grid(2, 1);
60+
///
61+
/// wnd.set_view(0, 0);
62+
/// wnd.draw_image(img, Some("Input Image"));
63+
///
64+
/// wnd.set_view(1, 0);
65+
/// wnd.draw_histogram(hst, 0.0, 255.0, Some("Input Image Histogram"));
66+
///
67+
/// wnd.show();
68+
///
69+
/// if wnd.is_closed().unwrap() == true { break; }
70+
/// }
71+
/// ```
3772
#[derive(Clone)]
3873
pub struct Window {
3974
handle: u64,
@@ -42,6 +77,7 @@ pub struct Window {
4277
cmap: ColorMap,
4378
}
4479

80+
/// Used to create Window object from native(ArrayFire) resource handle
4581
impl From<u64> for Window {
4682
fn from(t: u64) -> Window {
4783
Window {handle: t, row: -1, col: -1, cmap: ColorMap::DEFAULT}
@@ -61,6 +97,7 @@ impl Drop for Window {
6197
}
6298

6399
impl Window {
100+
/// Creates new Window object
64101
#[allow(unused_mut)]
65102
pub fn new(width: i32, height: i32, title: String) -> Result<Window, AfError> {
66103
unsafe {
@@ -75,6 +112,7 @@ impl Window {
75112
}
76113
}
77114

115+
/// Set window starting position on the screen
78116
pub fn set_position(&self, x: u32, y: u32) -> Result<(), AfError> {
79117
unsafe {
80118
let err_val = af_set_position(self.handle as WndHandle, x as c_uint, y as c_uint);
@@ -85,6 +123,7 @@ impl Window {
85123
}
86124
}
87125

126+
/// Set window title
88127
pub fn set_title(&self, title: String) -> Result<(), AfError> {
89128
unsafe {
90129
let err_val = af_set_title(self.handle as WndHandle,
@@ -96,10 +135,13 @@ impl Window {
96135
}
97136
}
98137

138+
/// Set color map to be used for rendering image, it can take one of the values of enum
139+
/// [ColorMap](./enum.ColorMap.html)
99140
pub fn set_colormap(&mut self, cmap: ColorMap) {
100141
self.cmap = cmap;
101142
}
102143

144+
/// Returns true if the window close is triggered by the user
103145
pub fn is_closed(&self) -> Result<bool, AfError> {
104146
unsafe {
105147
let mut temp: i32 = 1;
@@ -111,6 +153,7 @@ impl Window {
111153
}
112154
}
113155

156+
/// Used to setup display layout in multiview mode
114157
pub fn grid(&self, rows: i32, cols: i32) -> Result<(), AfError> {
115158
unsafe {
116159
let err_val = af_grid(self.handle as WndHandle, rows as c_int, cols as c_int);
@@ -121,6 +164,8 @@ impl Window {
121164
}
122165
}
123166

167+
/// Used in multiview mode to swap back buffer with front buffer to show the recently rendered
168+
/// frame
124169
pub fn show(&mut self) -> Result<(), AfError> {
125170
unsafe {
126171
let err_val = af_show(self.handle as WndHandle);
@@ -133,11 +178,14 @@ impl Window {
133178
}
134179
}
135180

181+
/// Used in multiview mode to set the current sub-region to which the subsequence draw call
182+
/// renders to
136183
pub fn set_view(&mut self, r: i32, c: i32) {
137184
self.row = r;
138185
self.col = c;
139186
}
140187

188+
/// Render given Array as an image
141189
pub fn draw_image(&self, input: &Array, title: Option<String>) {
142190
let tstr = match title {
143191
Some(s) => s,
@@ -154,6 +202,7 @@ impl Window {
154202
}
155203
}
156204

205+
/// Render given two Array's `x` and `y` as a 2d line plot
157206
pub fn draw_plot(&self, x: &Array, y: &Array, title: Option<String>) {
158207
let tstr = match title {
159208
Some(s) => s,
@@ -172,6 +221,7 @@ impl Window {
172221
}
173222
}
174223

224+
/// Render given Array as a histogram
175225
pub fn draw_hist(&self, hst: &Array, minval: f64, maxval: f64, title: Option<String>) {
176226
let tstr = match title {
177227
Some(s) => s,

0 commit comments

Comments
 (0)