File: star_selection.c

package info (click to toggle)
spacechart 0.9.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,704 kB
  • ctags: 917
  • sloc: ansic: 7,718; sh: 330; makefile: 205; sed: 93
file content (199 lines) | stat: -rw-r--r-- 5,879 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/***********************************************************************
 * star_selection.c : Implementation of the star "pattern" type.
 ***********************************************************************/

/***********************************************************************
 *  This file is part of SpaceChart.
 *  Copyright (C) 2000 Miguel Coca <e970095@zipi.fi.upm.es>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 ***********************************************************************/

#include <stdlib.h>
#include <strings.h>
#include "../include/starmap.h"
#include "../include/star.h"
#include "../include/star_selection.h"

#define MAX_SPEC 30

struct st_star_selection
{
        int use_min_lum;
        float min_lum;
        int use_max_lum;
        float max_lum;
        int use_spectrum;
        spectrum_t spectrum[MAX_SPEC];
        int allow_multiples;
        int tmp_matched;
};

/* Declaration of private functions. */
static void match_component( star_component_t *comp, star_selection_t *sel );

/* Public Functions. */

star_selection_t* star_selection_new( void )
{
        star_selection_t* selection;
        
        if( ( selection = 
              (star_selection_t*) malloc( sizeof( star_selection_t ) ) ) )
        {
                selection->use_min_lum = 0;
                selection->use_max_lum = 0;
                selection->use_spectrum = 0;
                selection->allow_multiples = 1;
        }
        return selection;
}

int star_selection_match( star_selection_t* selection, star_t* star )
{
        if( !selection->allow_multiples && (star_components(star) > 1) )
                selection->tmp_matched = 0;
        else
        {
                selection->tmp_matched = 0;
                star_foreach_component( star, 
                                        (void(*)(star_component_t*, void*)) 
                                        match_component, selection );
        }
        return selection->tmp_matched;
}

void star_selection_act_min_lum( star_selection_t* selection, float min_lum )
{
        selection->use_min_lum = 1;
        selection->min_lum = min_lum;
}

void star_selection_act_max_lum( star_selection_t* selection, float max_lum )
{
        selection->use_max_lum = 1;
        selection->max_lum = max_lum;
}

void star_selection_act_spectrum( star_selection_t* selection, 
                                  spectrum_t spectrum )
{
        selection->spectrum[selection->use_spectrum] = spectrum;
        selection->use_spectrum++;
}

void star_selection_deact_min_lum( star_selection_t* selection )
{
        selection->use_min_lum = 0;
}

void star_selection_deact_max_lum( star_selection_t* selection )
{
        selection->use_max_lum = 0;
}

void star_selection_deact_spectrum( star_selection_t* selection )
{
        selection->use_spectrum = 0;
}

int star_selection_is_min_lum( star_selection_t* selection )
{
        return selection->use_min_lum;
}

double star_selection_get_min_lum( star_selection_t* selection )
{
        return selection->min_lum;
}

int star_selection_is_max_lum( star_selection_t* selection )
{
        return selection->use_max_lum;
}

double star_selection_get_max_lum( star_selection_t* selection )
{
         return selection->max_lum;
}

int star_selection_is_spectrum( star_selection_t* selection )
{
        return selection->use_spectrum;
}

int star_selection_is_showed( star_selection_t* selection, 
                              spectrum_t spectrum )
{
        int i, found_sp = 0;
        if( selection->use_spectrum )
        {
                for( i = 0; i < selection->use_spectrum && !found_sp; i++ )
                        if( selection->spectrum[i] == spectrum )
                                found_sp = 1;
                return found_sp;
        }
        else
                return 1;
}

void star_selection_set_allow_multiples( star_selection_t* selection, 
                                        int allow )
{
        selection->allow_multiples = allow;
}

int star_selection_get_allow_multiples( star_selection_t* selection )
{
        return selection->allow_multiples;
}

void star_selection_copy( star_selection_t* orig, star_selection_t* new )
{
        *new = *orig;
}

void star_selection_destroy( star_selection_t* selection )
{
        free( selection );
}

/* Private functions. */

void match_component( star_component_t *comp, star_selection_t *sel )
{
        int matches = 1;
        int found_sp;
        int i;
        spectrum_t spectrum;
        
        if( sel->use_min_lum )
                matches = (sel->min_lum < star_component_get_luminosity(comp));
        
        if( sel->use_max_lum && matches )
                matches = (sel->max_lum>= star_component_get_luminosity(comp));

        if( sel->use_spectrum && matches )
        {
                spectrum = star_component_get_spectrum_type( comp );
                found_sp = 0;
                for( i = 0; i < sel->use_spectrum && !found_sp; i++ )
                        if( sel->spectrum[i] == spectrum )
                                found_sp = 1;
                matches = found_sp;
        }
        
        sel->tmp_matched = sel->tmp_matched || matches;
}